0

I'm new to android development but i have some Java background. In Java when using loop i used System.out.println(); but in Android after loop finishes, I only get the last result. I'm using JSON data which contains 10 lines.

Here's my code:

json = new JSONObject(result);
JSONArray articles = json.getJSONArray("entries"); // get articles array                

for(int i = 0; i <articles.length(); i++) {
    etResponse.setText(articles.getJSONObject(i).toString());
}

etResponse is my EditText and it only shows last line.

I have also tried:

builder.append(String.valueOf(articles.getJSONObject(i).toString()));

But it gave no data. So my question is how to get all lines data printed, not just the last one?

Anindya Dutta
  • 1,972
  • 2
  • 18
  • 33
  • **maybe** you forgot this logic . system.out.print() **+** \n = system.out.println(); ... so in normal java , each loop had a \n inbuilt if you used the function with **ln** for printing – Srinath Ganesh May 26 '15 at 14:45

5 Answers5

0

use append

 for (int i = 0; i < articles.length();i++) {
   etResponse.append(... + "\n");
 }
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • but how do you print only the values inside the JSON object. I have id 1, id 2 .. and so on and name John, name Bruce. So how do i print only those in line without anny {} [] "", just the values like "1 John Adress 555-555 " ? –  May 26 '15 at 15:39
0

How about using the StringBuilder to generate the string and then setting the value to etResponse outside the for loop ?

Jayesh Elamgodil
  • 1,467
  • 11
  • 15
0

When you do a etResponse.setText("new text") you're swapping the previous value with "new text".

Fix with saving your values in a StringBuilder and set them to the EditText once you're ready.

StringBuilder sb = new StringBuilder();
for(int i = 0;i <articles.length();i++) {
         long id = articles.getJSONObject(i).getLong("id");
         String name = articles.getJSONObject(i).getString("name");
         sb.append(id).append(" -> ").append(name).append("\n);
}

etResponse.setText(sb.toString());
Vesko
  • 3,750
  • 2
  • 23
  • 29
  • but how do you print only the values inside the JSON object. I have id 1, id 2 .. and so on and name John, name Bruce. So how do i print only those in line without anny {} [] "", just the values like "1 John Adress 555-555 " ? –  May 26 '15 at 15:39
  • Edited my answer to include this. Replace "id" and "name" with your keys in the JSON. – Vesko May 26 '15 at 15:42
0

The problem with your code is that you overwrite the current text every time your loop executes. So instead of setting the text in every iteration of the loop (somewhat inefficient to because the GUI gets updated a lot), you can store the text in a String variable and after the loop you can update the GUI with setText().

String result = "";

for(int i = 0;i <articles.length();i++){
     result += articles.getJSONObject(i).toString() + "\n";
}

etResponse.setText(result);
moffeltje
  • 4,521
  • 4
  • 33
  • 57
  • just to note that it is much better to use StringBuilder if you want to concatenate strings instead of just one String - it is much faster – Gabriella Angelova May 26 '15 at 14:45
  • Strings concatenation -> 11741 ms StringBuilder use -> 7 ms Take a look at this SO question - there is an example code for evaluating the time -> http://stackoverflow.com/questions/1532461/stringbuilder-vs-string-concatenation-in-tostring-in-java – Gabriella Angelova May 26 '15 at 14:50
  • but how do you print only the values inside the JSON object. I have id 1, id 2 .. and so on and name John, name Bruce. So how do i print only those without anny {} [] "", just the values ? –  May 26 '15 at 15:28
0

Convert your code to this one:

json = new JSONObject(result);
JSONArray articles = json.getJSONArray("entries"); // get articles array                
StringBuilder wholeText = new StringBuilder();
for(int i = 0;i <articles.length();i++){
   wholeText.append(articles.getJSONObject(i).toString() + "\n");
}
etResponse.setText(wholeText.toString());
Gabriella Angelova
  • 2,985
  • 1
  • 17
  • 30
  • but how do you print only the values inside the JSON object. I have id 1, id 2 .. and so on and name John, name Bruce. So how do i print only those in line without anny {} [] "", just the values like "1 John Adress 555-555 " ? –  May 26 '15 at 15:39