2017-02-13 116 views
0

我在我的RecyclerView中有一个句子,其中有3个TextView。图为象下面这样:在TextView Android中添加另一个字

enter image description here

在那张照片,我有一句话在3 TextView,有“1”“HOT,更辣椒”和“比萨”。这是我的RecyclerView绑定代码:

try { 
    view.txtArticlesName.setText(/*orderList.getJSONObject(position).getString("quantityValue") +*/ 
          /*orderList.getJSONObject(position).getString("spesial-request").replaceAll("[\\\"\\[\\]]", "") + */ 
            orderList.getJSONObject(position).getString("bezeich")); 
    view.txtQty.setText(orderList.getJSONObject(position).getString("quantityValue")); 
    view.txtReqList.setText(orderList.getJSONObject(position).getString("spesial-request").replaceAll("[\\\"\\[\\]]", "")); 

    } catch (JSONException e) { 
        e.printStackTrace(); 
    } 

我想参加所有的TextView只有一个“TextView`动态地。我会尝试这个办法:

view.txtArticlesName.setText(/*orderList.getJSONObject(position).getString("quantityValue") +*/ 
          /*orderList.getJSONObject(position).getString("spesial-request").replaceAll("[\\\"\\[\\]]", "") + */ 
            orderList.getJSONObject(position).getString("bezeich")); 

但它不能正常工作,它不是将数据绑定,所以TextView只显示默认的文本“Hello World”。 TextView可以做到这一点吗?我也读了Spannable,但我不知道它的工作如何在一个TextView中添加新词。或者有另一种方法可以做到这一点?任何建议和答案将有助于我。以前感谢。

+1

也许你有例外。并且此代码从不执行。只是看到日志猫或使用日志,以确保所有此代码执行 –

+0

@farazkhonsari我的日志没有显示任何错误。当我点击比萨或其他食物从另一个RecyclerView itss成功广告到这个RecyclerView,但TextView只显示“Hello World” –

回答

0

我解决了这个有一点点失望。在这里我的最终代码:

try { 
    view.txtArticlesName.setText(orderList.getJSONObject(position).getString("quantityValue") + " " + 
     orderList.getJSONObject(position).optString("spesial-request").replaceAll("[\\\"\\[\\]]", "") + " " + 
     orderList.getJSONObject(position).getString("bezeich")); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

用的getString:

with getString

与optString:

with optString

我改变getStringoptString和运营商+现在的工作。我不完全承认为什么getString不起作用,但optString工作。

非常感谢您对所有anwser,从+1我

1

在您的TextView中简单地连接字符串。

String string1 = "Hello", string2 = "HOT CHILLI", string3 = "PIZZA"; 

TextView textView = (TextView) findViewById(R.id.your_textView); 
textView.setText(string1.concat(string2).concat(string3)); 

或者您也可以将它附加到现有的TextView文本中。

textView.setText(textView.getText().toString.concat(string2)); 

编辑:

从字符串变量服务器收集的数据,然后这些变量传递给TextView的。

String string1, string2, string3; 


try { 
    string1 = orderList.getJSONObject(position).getString("quantityValue"); 
    string2 = orderList.getJSONObject(position).getString("spesial-request").replaceAll("[\\\"\\[\\]]", ""); 
    string3 = orderList.getJSONObject(position).getString("bezeich")); 
    String final = string1.concat(string2).concat(string3); 
    view.txtView.setText(final); 
} catch (JSONException e) { 
    e.printStackTrace(); 
} 
+0

它不工作,我'TextView'不绑定从我的服务器的数据 –

+0

@MarioMargoPradipta检查我的编辑。 – Rachit

+0

感谢您的回答,如果我们仍然使用'getString',我认为它不起作用。我在下面发表我的答案。再次感谢 –

1

使用'+'运营商或StringBuilder加入2个或更多字符串和结果字符串设定成一个单一的TextView。

如果你想有不同的字体,颜色,大小,粗体等文本的某些部分,可以使用Spannable字符串或Html.fromHtml()

+0

+运营商它不适合我,我不知道为什么。我会尝试你的关于String Builder的建议 –

1

可以制造和使用spannable字符串象下面这样:

private void addSpannableString(){ 

     //"1" "HOT, MORE CHILLI" and "Pizza" 
     String one= "1"; 
     String two= "HOT, MORE CHILLI"; 
     String three= "Pizza"; 
     String mergeString= one + two + three; 
     Spannable spannable = new SpannableString(mergeString); 
     StyleSpan boldSpan = new StyleSpan(Typeface.BOLD); 
     spannable.setSpan(boldSpan, mergeString.indexOf(two), mergeString.indexOf(three), Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
     textview.setText(mergeString); 
    } 

有很多方法可以使Spans包括颜色和字体

1

使用TextView.append(CharSequence text)向其添加更多文本。

追加指定的文本TextView的的显示缓冲区,升级 它BufferType.EDITABLE如果不是已经编辑


1

GetStringOptString之间的区别是:

Documentation

OptString返回空字符串( “”),如果您指定的密钥不存在 。另一方面,GetString抛出一个JSONException。

使用getString如果它是一个错误的数据丢失,或者optString如果你不知道它会在那里。

+0

@Mario Margo Pradipta检查解释.. – rafsanahmad007

+0

如果那样,为什么我们不总是使用'optString'? –

+0

当数据有必要显示..应该使用Getstring ..它也有助于调试..如果JOSN中缺少任何值 – rafsanahmad007

相关问题