0

I have one sentence with 3 TextView in my RecyclerView. The picture is like below :

enter image description here

In that picture, I have one sentence in 3 TextView, there are "1" "HOT, MORE CHILLI" and "Pizza". This is my RecyclerView Binding code :

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();
   }

I want to Join all of TextView with only one 'TextView` dynamicly. I'll try this :

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

But its not work, its not bind the data, so the TextView just show the default text "Hello World". Can TextView do this? I read about Spannable too but i dont know how its work to add new word in one TextView. Or there is another way to do this? Any suggest and answer will helpfull for me. Thanks before.

MrX
  • 953
  • 2
  • 16
  • 42
  • 1
    maybe you have exception. and this code is never executed. just see the log cat or use Log to ensure that all this code is executed – faraz khonsari Feb 13 '17 at 07:52
  • @farazkhonsari My Log not show anything wrong. When i click pizza or another food from another RecyclerView itss success ad to this RecyclerView but the TextView just only show "Hello World" – MrX Feb 13 '17 at 07:58

6 Answers6

2

Simply concat the Strings in your TextView.

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

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

Or you could also append it to the existing TextView's text.

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

EDIT:

Collect the data from the server in String variables, and then pass those variables to the 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();
}
Rachit
  • 3,173
  • 3
  • 28
  • 45
  • its not work, my `TextView` not bind the data from my server – MrX Feb 13 '17 at 07:55
  • thanks for answer, i think its not work if we still use `getString`. I post my answer below. Thanks again – MrX Feb 13 '17 at 08:22
1

Use '+' operator or StringBuilder to join 2 or more strings and set the resultant string into a single textview.

If you want to have some part of text with different font, color, size, bold etc, you can use Spannable string or Html.fromHtml()

Gaurav Chauhan
  • 376
  • 3
  • 14
  • + operator its not work for me and i dont know why. I will try your suggest about String Builder – MrX Feb 13 '17 at 07:58
1

you can make and use spannable String like below :

 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);
    }

There are many methods are available to make Spans including colors and Typefaces

Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
1

Use TextView.append(CharSequence text) to add more text to it.

Append the specified text to the TextView's display buffer, upgrading it to BufferType.EDITABLE if it was not already editable


Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
1

The Difference between GetString And OptString is:

From Documentation

OptString returns the empty string ("") if the key you specify doesn't exist. GetString on the other hand throws a JSONException.

Use getString if it's an error for the data to be missing, or optString if you're not sure if it will be there.

rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
  • if that so, why we dont always use `optString` ? – MrX Feb 13 '17 at 08:46
  • When data is necessary to show..should use Getstring..it is also helpful for debug..if any values is missing in JOSN – rafsanahmad007 Feb 13 '17 at 08:47
  • My `TextView` show "Hello World" because there is no key string with "quantityValue", i got it now. Thanks for great explantion +1 – MrX Feb 13 '17 at 08:50
0

I solved this with a little disappointed . Here my final code :

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();
    }

with getString :

with getString

with optString :

with optString

I change getString with optString and operator + work now. I'm not fully undertand why getString not work but optString work.

Thank you very much for all anwser, +1 from me

MrX
  • 953
  • 2
  • 16
  • 42