首页 > 解决方案 > 如何将json响应添加到分段recyclerview android

问题描述

我正在创建分段recyclerview 列表。使用服务器响应一个用于标题,第二个用于子级如何在我的分段recyclerview 中动态添加它但是当我在recyclerview 中获取输出时,最后一条记录插入了两次子列表。

像这样来自服务器的两个 json 数组

 "status": "1",
    "msg": "Success",
    "resident":  {[
            "wing_id": "4",

            "resident_details": [
                {
                    "first_name": "Suraj",
                    "last_name": "Jumbad",

                    "wing_id": "4",

                },
                {
                    "first_name": "Laxman ",
                    "last_name": "Kathar",


                    "wing_id": "4",

                }
            ]
        }

这是我的代码

  try {
                        if (response.has("status") && response.getString("status").equals("1")) {
                            JSONArray residents = response.getJSONArray("resident");
                            for (int i = 0; i < residents.length(); i++) {

                                JSONObject jsonObject_residents_details = residents.getJSONObject(i);
                                String wing_name = jsonObject_residents_details.getString("wing_name");
                                String wing_id = jsonObject_residents_details.getString("wing_id");


                                sectionHeaderss.add(new SectionHeader(child, wing_name, i));
                                JSONArray resident_details = jsonObject_residents_details.getJSONArray("resident_details");
                                for (int j = 0; j < resident_details.length(); j++) {
                                    JSONObject jsonObject = resident_details.getJSONObject(j);
                                    String wing_ids = jsonObject.getString("wing_id");
                                    String first_name = jsonObject.getString("first_name");
                                    childList.add(new Child(first_name, wing_ids, wing_name));
                                    //child add
                                    //sectionHeaderss.add(new SectionHeader(childList, wing_name, i));

                                }
                                //main add

                                if (!childList.isEmpty()) {
                                    for (int k = 0; k < childList.size(); k++) {
                                        if (childList.get(k).getId().equals(wing_id)) {

                                            sectionHeaderss.add(new SectionHeader(childList, wing_name, i));

                                        }

                                    }
                                } else {
                                    sectionHeaderss.add(new SectionHeader(child, wing_name, i));
                                }

                            }


                            adapterRecycler = new AdapterSectionRecycler(GroupListActivity.this, sectionHeaderss);
                            rv_resident_list.setAdapter(adapterRecycler);
                            if (adapterRecycler != null) {
                                adapterRecycler.notifyDataSetChanged();
                            }
                        } else {
                            Toast.makeText(GroupListActivity.this, response.get("msg").toString(), Toast.LENGTH_SHORT).show();


                        }
                    } catch (Exception e) {

                        Toast.makeText(GroupListActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();

                    }

                    Functions.pbVisiblity(true, pb_rv_resident_list);
                }

标签: androidandroid-recyclerviewsectionedrecyclerviewadapter

解决方案


推荐阅读