1

How do I get and show each and every JSON's Object's value those I have inside an Array called learning

Here is how JSON Array looks like:

"learning": [
 {
  "code":"2K14 - 2",
  "os":"Windows - 2"
 },
 {
  "code":"2K15 - 2",
  "os":"Linux - 2"
 },
 {
  "code":"2K16 - 2",
  "os":"Mac - 2"
 }
]

Code

List<Learning> learning = value.getLearning();
for(Learning m : learning) {
     // I guess, here I am missing something, which is really useful
     String code = m.getCode();
     String os = m.getOs();
     viewHolder.learning.setText("Code: "+code+" OS: "+os);
}

When I execute my program, getting this:

Code: 2k16, OS: Mac - 2

Whereas I want to get something like this:

Code: 2k14 OS: Windows - 2,  Code: 2k15 OS: Linux - 2, Code: 2k16 OS: Mac - 2
Oreo
  • 2,586
  • 8
  • 38
  • 63
  • how are you parsing the json ? – Blackbelt Jan 21 '16 at 13:24
  • `viewholder`, im assuiming you are using the Viewholder pattern within a list adapter. No need for loop. GetView runs multiple times to render the list items. It should display an item based on position. Look into `getItem(position)`. – TheSunny Jan 21 '16 at 13:24
  • using Retrofit @Blackbelt – Oreo Jan 21 '16 at 13:25
  • `viewHolder.learning.setText("Code: "+code+" OS: "+os);` is replacing the text. You need something along the lines of `viewHolder.learning.setText( viewHolder.learning.getText() + "Code: "+code+" OS: "+os);` or `viewHolder.learning.appendText(" Code: "+code+" OS: "+os);`. Not sure what methods are available to you, but that's the problem ... – Jonas Schubert Erlandsson Jan 21 '16 at 13:30

3 Answers3

2

Do it as, if want to show all data in single TextView:

viewHolder.learning.append("Code: "+code+" OS: "+os + ", ");

Use TextView.append instead of TextView.setText

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • thanks append() worked for me, I ticked your answer as useful and will accept in 7 mins, may I know How can I show comma between objects as shown above – Oreo Jan 21 '16 at 13:31
  • please have a look at this issue: http://stackoverflow.com/questions/35355559/populate-data-into-recyclerview – Oreo Feb 12 '16 at 06:39
1

Can you post your Retrofit interface and all the model classes related to that?

and then you will have something like this to read it.

String text = "";
List<Learning> learning = value.getLearning();
for(Learning m : learning) {
     // I guess, here I am missing something, which is really useful
     String code = m.getCode();
     String os = m.getOs();
     text = text + "Code: " + code + " OS: " + os + ", ";
}
viewHolder.learning.setText(text);
tasomaniac
  • 10,234
  • 6
  • 52
  • 84
  • 2
    thank you so much, I ticked your answer as useful, but @ρяσѕρєяK answered first, therefore I'll accept his solution :) – Oreo Jan 21 '16 at 13:39
  • please have a look at this issue: http://stackoverflow.com/questions/35355559/populate-data-into-recyclerview – Oreo Feb 12 '16 at 06:39
0
JSONArray jsonArray = jsonObj.getJSONArray("learning");

for(int i = 0; i < jsonArray.length(); i++)
{
    JSONObject obj  = jsonArray.getJSONObject(i);
    String code = obj.getString("code");
    String os = obj.getString("os");

    Log.i("MyClass", "Code -> " + code);
    Log.i("MyClass", OS -> " + os);

}
Mustansar Saeed
  • 2,730
  • 2
  • 22
  • 46