为什么只显示回收程序视图中的最后一项?

Why is the last item in recycler view only displayed?

提问人:Ehsan Taghizadeh 提问时间:10/22/2023 更新时间:10/22/2023 访问量:30

问:

我在 mocki.io 中创建 json 数组并从中获取数据 运行应用时,将我的 JSON 数组中的最后一项显示到 Recycler 列表中 只需在模拟器中显示所有项目中的最后一项 如何解决这个问题?

如何从我的数据中获取第一项并显示在我的回收器中的零位置?

public class MainActivity extends AppCompatActivity {
    RecyclerView recyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = findViewById(R.id.recycler);
        List<PeopleModel> peopleModelList = new ArrayList<>();
        StringRequest stringRequest = new StringRequest(Request.Method.GET,
                "https://mocki.io/v1/59c9548b-4cde-48d8-9d60-f25444944b81", new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                PeopleModel peopleModel = new PeopleModel();
                try {
                    JSONArray jsonArray = new JSONArray(response);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        peopleModel.setFirstName(jsonObject.getString("first_name"));
                        peopleModel.setLastName(jsonObject.getString("last_name"));
                        peopleModel.setId(String.valueOf(jsonObject.getInt("id")));
                        peopleModel.setCity(jsonObject.getString("city"));
                        peopleModelList.add(peopleModel);

                    }
                    recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this,
                            RecyclerView.VERTICAL,false));
                    recyclerView.setAdapter(new PeopleAdapter(peopleModelList));
                    Log.i("TAG", "onResponse: "+response);

                } catch (JSONException e) {
                    throw new RuntimeException(e);
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);

    }
}
java android json

评论


答:

0赞 Ashok 10/22/2023 #1

这是 Object by Reference 的经典问题,请注意,在代码库中,您只创建了一个对象,然后您只是更新对象的内部值并在 List 中附加相同的对象。因此,List 多次包含相同的对象引用,并且在每个循环中,您都会更新更新同一对象的数据。PeopleModel peopleModel = new PeopleModel();

要解决此问题,您需要为列表中的每个项目创建一个新的 PeoplerModel,因此您需要在 for 循环内而不是在外部启动 peopleModel。


            public void onResponse(String response) {
                try {
                    JSONArray jsonArray = new JSONArray(response);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        PeopleModel peopleModel = new PeopleModel();

                        peopleModel.setFirstName(jsonObject.getString("first_name"));
                        peopleModel.setLastName(jsonObject.getString("last_name"));
                        peopleModel.setId(String.valueOf(jsonObject.getInt("id")));
                        peopleModel.setCity(jsonObject.getString("city"));
                        peopleModelList.add(peopleModel);

                    }
                    recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this,
                            RecyclerView.VERTICAL,false));
                    recyclerView.setAdapter(new PeopleAdapter(peopleModelList));
                    Log.i("TAG", "onResponse: "+response);

                } catch (JSONException e) {
                    throw new RuntimeException(e);
                }

            }