最后的数据始终显示在ListView中

时间:2017-06-20 05:23:27

标签: android json listview

以下是我的示例json结果

[  
 {
  "ApplicationId": "1609000001",
  "Status": "Submitted"
 },
 {
  "ApplicationId": "1609000002",
  "Status": "Received"
 },
{
  "ApplicationId": "1609000004",
  "Status": "Draft"
},
]

我使用Volley获取json结果。我正确地获得了值,但ListView上显示的项目仍然显示最后一项,例如基于上面的示例json数据1609000004它返回相同的值。与例如3的结果一样,它将显示3 1609000004个应用程序ID。

这是我的MainActivity

 ArrayList<Notification> submissionList; 
 ListView status_lv;

 onCreate(Bundle savedInstanceState) {
   status_lv = (ListView) findViewById(R.id.status_lv);
    statusList = new ArrayList<Notification>();

    callVolley();
    StatusAdapter adapter = new StatusAdapter(MainActivity.this, R.layout.listrow, statusList);
    status_lv.setAdapter(adapter);
   }

     private void callVolley() {
    JsonArrayRequest req = new JsonArrayRequest(Constants.URL,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {

                try {
                    Notification status = new Notification();

                    for (int i = 0; i < 5; i++) {

                        JSONObject submissionObj = (JSONObject) response.get(i);
  status.setApplicationId(submissionObj.getString("ApplicationId"));
  status.setStatus(submissionObj.getString("Status"));

                 statusList.add(status);      
                    }

                } catch (JSONException e) {
                    e.printStackTrace();

                }

        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });
    requestQueue.add(req);
}

在我的for loop内部检查后,我尝试放置Log并获得正确的数据ApplicationId为1609000001,1609000002,1609000004。但我不知道为什么在我的设备中显示最后一个值。感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

在for循环后设置适配器

ArrayList<Notification> submissionList; 
ListView status_lv;

onCreate(Bundle savedInstanceState) {
    status_lv = (ListView) findViewById(R.id.status_lv);

    callVolley();

}

private void callVolley() {
    statusList = new ArrayList<Notification>();

    JsonArrayRequest req = new JsonArrayRequest(Constants.URL,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {

                try {


                    for (int i = 0; i < response.length(); i++) {
                        Notification status = new Notification();
                        JSONObject submissionObj = (JSONObject) response.get(i);
                        status.setApplicationId(submissionObj.getString("ApplicationId"));
                        status.setStatus(submissionObj.getString("Status"));

                        statusList.add(status);      
                    }

                    StatusAdapter adapter = new StatusAdapter(MainActivity.this, R.layout.listrow, statusList);
                    status_lv.setAdapter(adapter);

                } catch (JSONException e) {
                    e.printStackTrace();

                }

        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });
    requestQueue.add(req);
}

答案 1 :(得分:1)

添加代码行

Notification status = new Notification();

在循环内部如下:

for (int i = 0; i < 5; i++) {

                        JSONObject submissionObj = (JSONObject) response.get(i);

Notification status = new Notification();
  status.setApplicationId(submissionObj.getString("ApplicationId"));
  status.setStatus(submissionObj.getString("Status"));

                 statusList.add(status);      
                    }

也改变循环添加&#34; response.size&#34;代替i&lt; 5否则你可能会得到&#34; outOfIndexException&#34;。