Search code examples
javaandroidsettext

Android setText in for loop to hold JsonObject values overwriting itself


I have been fiddling with this for a few hours, essentially what I am trying to do is create a JsonObject and get it's value inside a for loop and Set it to a TextView. It is working however I am getting only the Second loop values, to clarify this is the output from my JsonObject

{"Areas":[{"City":"Dallas","State":"Texas"},{"City":"Seattle","State":"Washington"}]}

However I am only getting {"City":"Seattle","State":"Washington"} the second values in the loop inside my Textviews setText. I would like to get both the first and second loop inside my Textview.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mystates);
    try {
  // Create my Json Array
        JSONObject newJsonObject = new JSONObject();

        JSONArray myarray = new JSONArray();
        JSONObject states= new JSONObject();
        states.put("City", "Dallas");
        states.put("State", "Texas");
       JSONObject states2= new JSONObject();
        states2.put("City", "Seattle");
        states2.put("State", "Washington"); 
        myarray.put(states);
        myarray.put(states2);
        newJsonObject.put("Areas", array);
 // Ends Json Array

        //set Textviews
        TextView citiess = (TextView)findViewById(R.id.citiess);
        TextView statess = (TextView)findViewById(R.id.statess);
 String cityString="";
 String stateString="";
       // retrieve Json Array and set For Loop
        JSONArray jaLocalstreams = names.getJSONArray("Areas");
        for(int position=0;position<jaLocalstreams.length();position++)
        {
        JSONObject JO = jaLocalstreams.getJSONObject(position);
        cityString= JO.getString("City");
         stateString = JO.getString("State");
            citiess.setText(""+cityString);
            statess.setText(""+stateString);
    }
    } 
  catch (Exception e) {
        e.printStackTrace();
    }

}

I have pretty much narrowed down the problem area and I think it is this

        citiess.setText(""+cityString);
        statess.setText(""+stateString);

setText is replacing the values with the newest ones which makes sense, how can I change this so that it does not do that? When I put this in the for loop

System.err.println(cityString+","+stateString);

I get both loop values of Dallas,Texas and Seattle,Washington. Which is why I am sure that setText is the issue any suggestions would be great.


Solution

  • Its seems that cityString,stateString get overwrite on loop and setText will print last value only.Therfore you need to append json values in string and then set those values after loop as

    for (int position = 0; position < jaLocalstreams.length(); position++) {
        JSONObject JO = jaLocalstreams.getJSONObject(position);
        cityString += JO.getString("City");
        stateString += JO.getString("State");
    }
    
    citiess.setText("" + cityString);
    statess.setText("" + stateString);