为什么我的jsonarray结果仅将一个记录存储到数组中?

时间:2019-06-11 19:52:24

标签: java android arraylist

这是我的截击任务,我想将响应数据存储到arraylist。我在控制台中得到了整个结果,但是在arraylist中,它仅存储第一条记录

private void loadJsonArray() {
        JsonArrayRequest jsArrayRequest = new JsonArrayRequest
                (Request.Method.GET, obj_url, null, new Response.Listener<JSONArray>() {


                    Questions questions = new Questions();
ArrayList<Questions> contactLists = new ArrayList<>();
                    @Override
                    public void onResponse(JSONArray responseArray) {

                        try {

                            for (int i = 0; i < responseArray.length(); i++) {
                                JSONObject response = responseArray.getJSONObject(i);
      Log.v("Naveen", response.toString());                       
                                questions.setId(response.getInt("employee_id"));
                                questions.setQuestions(response.getString("name"));
                                questions.setOption1(response.getString("dob"));
                                questions.setOption2(response.getString("designation"));
                                questions.setOption3(response.getString("contact_number"));
                                questions.setAnswerNr(response.getInt("email"));
                                questions.setDifficulty(response.getString("salary"));

                                contactLists.add(questions);
                                Log.v("Kumar", contactLists.toString());


                            }


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


                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {


                        //Display error message whenever an error occurs
                        Toast.makeText(getApplicationContext(),
                                error.getMessage(), Toast.LENGTH_SHORT).show();

                    }
                });

        // Access the RequestQueue through your singleton class.
        MySingleton.getInstance(this).addToRequestQueue(jsArrayRequest);

    } 

和我的控制台:

    06-12 00:45:22.863 6930-6930/com.codinginflow.myawesomequiz V/Naveen: {"employee_id":"1","name":"what is php","dob":"1994-08-28","designation":"Systems Engineer","contact_number":"8888888888","email":"1","salary":"easy"}
    06-12 00:45:22.863 6930-6930/com.codinginflow.myawesomequiz V/Naveen: {"employee_id":"2","name":"what is html","dob":"1989-03-01","designation":"Technology Lead","contact_number":"7777777777","email":"2","salary":"easy"}
    06-12 00:45:22.863 6930-6930/com.codinginflow.myawesomequiz V/Naveen: {"employee_id":"3","name":"what is java","dob":"1990-05-20","designation":"Technology Consultant","contact_number":"7888888888","email":"3","salary":"easy"}
    06-12 00:45:22.864 6930-6930/com.codinginflow.myawesomequiz V/Naveen: {"employee_id":"4","name":"what is ip","dob":"1993-08-25","designation":"Systems Engineer","contact_number":"7999999999","email":"1","salary":"easy"}
    06-12 00:45:22.864 6930-6930/com.codinginflow.myawesomequiz V/Naveen: {"employee_id":"3","name":"what is java","dob":"1990-05-20","designation":"Technology Consultant","contact_number":"7888888888","email":"3","salary":"easy"}
06-12 00:45:22.864 6930-6930/com.codinginflow.myawesomequiz V/Kumar: [com.codinginflow.myawesomequiz.Questions@2885909, com.codinginflow.myawesomequiz.Questions@2885909, com.codinginflow.myawesomequiz.Questions@2885909, com.codinginflow.myawesomequiz.Questions@2885909, com.codinginflow.myawesomequiz.Questions@2885909]

它只存储第一条记录,而多次,我如何将所有记录存储到arraylist中?

3 个答案:

答案 0 :(得分:0)

我更喜欢每次循环。看起来比较干净。解决方案是您需要在所有迭代中创建新对象

for (Questions question: responseArray){
Questions questions = new Questions();
questions.setId(response.getInt("employee_id"));
                            questions.setQuestions(question.getString("name"));
                            questions.setOption1(question.getString("dob"));
                            questions.setOption2(question.getString("designation"));
                            questions.setOption3(question.getString("contact_number"));
                            questions.setAnswerNr(question.getInt("email"));
                            questions.setDifficulty(question.getString("salary"));

                            contactLists.add(questions);

答案 1 :(得分:0)

您好,您似乎一遍又一遍地重复使用相同的Questions实例,这会导致列表中填充相同实例的重复项。每次填充对象时,尝试创建一个新实例:

for (int i = 0; i < responseArray.length(); i++) {
    Questions questions = new Questions();
    [...]
    contactLists.add(questions);
}

答案 2 :(得分:0)

您应该在其中创建一个新的Questions对象-否则,您将覆盖之前创建的对象。

 for (int i = 0; i < responseArray.length(); i++) {
Questions questions = new Questions();
                                JSONObject response = responseArray.getJSONObject(i);
      Log.v("Naveen", response.toString());                       
                                questions.setId(response.getInt("employee_id"));
                                questions.setQuestions(response.getString("name"));
                                questions.setOption1(response.getString("dob"));
                                questions.setOption2(response.getString("designation"));
                                questions.setOption3(response.getString("contact_number"));
                                questions.setAnswerNr(response.getInt("email"));
                                questions.setDifficulty(response.getString("salary"));

                                contactLists.add(questions);
                                Log.v("Kumar", contactLists.toString());