I am working on an android project that will get a Json array from a server and then I want to iterate through the array and extract data from each Json object in the array. When I run this code the textviews do not change, but if I remove the for loop and everything in it, and then uncomment the two lines below the for loop, things seems to work. How can you iterate through the Json objects in the array so that the info will be updated on the screen and change every second for each object in the array?
Thank you in advance!
name = (TextView)findViewById(R.id.nameTxt);
email = (TextView)findViewById(R.id.emailTxt);
serverButton = (Button)findViewById(R.id.getInfoBtn);
serverButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(server_url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
for (int i = 0; i < response.length(); i++) {
JSONObject nameJson = response.getJSONObject(i);
name.setText(nameJson.getString("FirstName") + " " + nameJson.getString("LastName"));
email.setText(nameJson.getString("Email"));
Thread.sleep(1000);
}
//JSONObject nameJson = response.getJSONObject(1);
// name.setText(nameJson.getString("FirstName"));
} catch (JSONException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
name.setText("Something is wrong");
}
});
MySingleton.getInstance(MainActivity.this).addToRequestQueue(jsonArrayRequest);