动态可扩展Listview问题

时间:2019-05-06 07:48:08

标签: android json adapter expandablelistview jsonparser

我正在尝试在可扩展列表视图中实现动态值,但是在每个主要项目中添加子项时遇到问题。问题在于所有子项目都被添加到每个主要项目中,而不是被添加到其各自的主要项目中。这是我正在使用的ArrayList的初始化-

 DayBookAdapter adapter;
    ArrayList<ArrayList<DayBookDetailsData>> ch_list = new ArrayList<ArrayList<DayBookDetailsData>>();

    ArrayList<DayBookData> list = new ArrayList<>(); 

这是JSON解析并添加主要项和子项的代码-

 try
        {
            JSONObject jsonrootobjct=new JSONObject(response);
            JSONArray jsonArray = null;
            jsonArray=jsonrootobjct.getJSONArray("data");

            for (int i = 0; i < jsonArray.length(); i++) {

                JSONObject jsonObject1 = null;
                jsonObject1= jsonArray.getJSONObject(i);
                try {
                    category_id = jsonObject1.getString(Json_categoryid);
                    category_name = jsonObject1.getString(Json_categoryname);

                    JSONArray jsonArrayitem=null;
                    jsonArrayitem = jsonObject1.getJSONArray("itemdata");
                    for (int j = 0; j < jsonArrayitem.length(); j++) {
                        jsonObject1 = jsonArrayitem.getJSONObject(j);
                        try {
                            item_name = jsonObject1.getString(Json_itemname);
                            item_id = jsonObject1.getString(Json_itemid);
                            DayBookDetailsData ch = new DayBookDetailsData(item_name, item_id);
                            ch_list.add(ch);
                        }
                        catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    DayBookData gru = new DayBookData(category_id,category_name, ch_list);
                    list.add(gru);
                    adapter=new DayBookAdapter(getApplicationContext(), list);
                    exp_listview.setAdapter(adapter);

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

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

这是我的DayBookData代码-

public class DayBookData {


    private String category_name;
    private String category_id;
    private ArrayList<DayBookDetailsData> ch_list;

    public String getCategory_id() {
        return category_id;
    }

    public String getCategory_name() {
        return category_name;
    }

    public ArrayList<DayBookDetailsData> getCh_list() {
        return ch_list;
    }

    public DayBookData(String category_id, String category_name, ArrayList<DayBookDetailsData> ch_list) {
        this.category_id=category_id;
        this.category_name=category_name;
        this.ch_list=ch_list;
    }

    public ArrayList<DayBookDetailsData> getItems() {
        return ch_list;
    }
}

这是我的DayBookDetailsData代码-

public class DayBookDetailsData {


    String item_name;
    String item_id;


    public String getItem_name() {
        return item_name;
    }

    public String getItem_id() {
        return item_id;
    }



    public DayBookDetailsData( String item_name, String item_id) {
        this.item_name=item_name;
        this.item_id=item_id;

    }
}

我的DaybookAdapter代码是-

public class DayBookAdapter  extends BaseExpandableListAdapter {

    private Context c;
    private ArrayList<DayBookData> list;
    private LayoutInflater inflater;

    public DayBookAdapter(Context c, ArrayList<DayBookData> list)
    {
        this.c=c;
        this.list=list;
        inflater=(LayoutInflater ) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        //return team.size();
        return list.size();
    }
    @Override
    public int getChildrenCount( int listPosition) {
        // TODO Auto-generated method stub
        //return team.get(groupPosw).players.size();
      ArrayList<DayBookDetailsData> chList = list.get(listPosition).getItems();
        return chList.size();
    }

    @Override
    public Object getGroup(int listPosition) {
        // TODO Auto-generated method stub
        //return team.get(groupPos);
        return list.get(listPosition);
    }

    @Override
    public Object getChild(int listPosition, int expandedListPosition) {
        // TODO Auto-generated method stub
        //return team.get(groupPos).players.get(childPos);

        ArrayList<DayBookDetailsData> chList =  list.get(listPosition).getItems();
        return chList.get(expandedListPosition);
    }

    @Override
    public long getGroupId(int listPosition) {
        // TODO Auto-generated method stub
        //return 0;
        return listPosition;
    }

    @Override
    public long getChildId(int listPosition, int expandedListPosition) {
        // TODO Auto-generated method stub
        //return 0;
        return expandedListPosition;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    public View getGroupView(int listPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        DayBookData group = (DayBookData) getGroup(listPosition);
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) c
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.day_book_data_item,null);
        }

        TextView category_id=(TextView) convertView.findViewById(R.id.category_id);
        TextView category_name=(TextView) convertView.findViewById(R.id.category_name);
        category_id.setText(group.getCategory_id());
        category_name.setText(group.getCategory_name());
        return convertView;
    }

    @Override
    public View getChildView(int listPosition, int expandedListPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if(expandedListPosition ==0)
        {
           convertView = inflater.inflate(R.layout.child_header, null);
            //TextView txtHeader = (TextView)convertView.findViewById(R.id.txtHeader);
            //txtHeader.setText("Hello");

        }
        if(expandedListPosition>0 && expandedListPosition<getChildrenCount(listPosition)-1) {

            DayBookDetailsData child = (DayBookDetailsData) getChild(listPosition, expandedListPosition);
            LayoutInflater layoutInflater = (LayoutInflater) this.c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.day_book_details_data_item, null);
            TextView item_name = (TextView) convertView.findViewById(R.id.item_name);
            TextView item_id = (TextView) convertView.findViewById(R.id.item_id);
            item_name.setText(child.getItem_name());
            item_id.setText(child.getItem_id());
        }

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int arg0, int arg1) {
        // TODO Auto-generated method stub
        return true;
    }
}

我的JSON数据看起来像这样-

"data":[
{
"c_name":"Stage And Platform",
"cat_id":"29",
"itemdata":[
{
"id":"2",
"item":" Wooden Stage"
},
{
"id":"3",
"item":" Iron Stage"
},
{
"id":"4",
"item":"Round Stage"
},
{
"id":"5",
"item":" Rotating Stage"
},
{
"id":"6",
"item":" Hydraulic Stage"
},
{
"id":"7",
"item":"Hydro-Rotating Stage"
},
{
"id":"8",
"item":"Wooden Dance Floor"
},
{
"id":"9",
"item":"Acrylic Dance Floor"
},
{
"id":"10",
"item":"Led Dance Floor"
},
{
"id":"466",
"item":"Podium"
}
]
},
{
"c_name":" Bar Set Up",
"cat_id":"30",
"itemdata":[
{
"id":"11",
"item":"Bar Chair"
},
{
"id":"12",
"item":"Bar Table"
},
{
"id":"13",
"item":"Bar Counter Wooden "
},
{
"id":"14",
"item":" Acrylic Bar Counter "
},
{
"id":"15",
"item":"Led Bar Counter"
},
{
"id":"16",
"item":"Bartender"
},
{
"id":"17",
"item":"Bar Utensils"
},
{
"id":"18",
"item":"Glass Bar Table"
}
]
}

请帮助。预先谢谢你。

1 个答案:

答案 0 :(得分:1)

我刚刚检查了您的代码,您使用了错误的方式来解析json并使用适配器绑定ExpandableListView

我已经更正了一些代码,现在可以正常工作了。请检查以下代码。

Json解析方法

public void parseJsonData(String response) {

    try {

        ArrayList<DayBookData> listData = new ArrayList<>();

        JSONObject jObjMain = new JSONObject(response);
        JSONArray jArrData = jObjMain.getJSONArray("data");

        for (int i = 0; i < jArrData.length(); i++) {

            DayBookData bData = new DayBookData();

            JSONObject jObjIn = new JSONObject(jArrData.getString(i));

            bData.setCategory_id(jObjIn.getString("cat_id"));
            bData.setCategory_name(jObjIn.getString("c_name"));

            JSONArray jArrItemData = new JSONArray(jObjIn.getString("itemdata"));

            for (int j = 0; j < jArrItemData.length(); j++) {

                JSONObject jObjItem = new JSONObject(jArrItemData.getString(j));

                try {

                    String item_id = jObjItem.getString("id");
                    String item_name = jObjItem.getString("item");

                    bData.getCh_list().add(new DayBookDetailsData(item_name, item_id));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            listData.add(bData);
        }

        exp_listview.setAdapter(new DayBookAdapter(context, listData));

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

DayBookData.java

public class DayBookData {

    private String category_name;
    private String category_id;
    private ArrayList<DayBookDetailsData> ch_list;

    public DayBookData() {
        this.category_id = "";
        this.category_name = "";
        this.ch_list = new ArrayList<>();
    }

    public String getCategory_name() {
        return category_name;
    }

    public void setCategory_name(String category_name) {
        this.category_name = category_name;
    }

    public String getCategory_id() {
        return category_id;
    }

    public void setCategory_id(String category_id) {
        this.category_id = category_id;
    }

    public ArrayList<DayBookDetailsData> getCh_list() {
        return ch_list;
    }

    public void setCh_list(ArrayList<DayBookDetailsData> ch_list) {
        this.ch_list = ch_list;
    }
}

DayBookDetailsData.java

public class DayBookDetailsData {

    private String item_name;
    private String item_id;

    public DayBookDetailsData(String item_name, String item_id) {
        this.item_name = item_name;
        this.item_id = item_id;
    }

    public String getItem_name() {
        return item_name;
    }

    public String getItem_id() {
        return item_id;
    }
}

DatBookAdapter.java

public class DayBookAdapter extends BaseExpandableListAdapter {

    private Context _context;
    private List<DayBookData> listDataHeader;

    public DayBookAdapter(Context context, List<DayBookData> listDataHeader) {
        this._context = context;
        this.listDataHeader = listDataHeader;
    }

    @Override
    public ArrayList<DayBookDetailsData> getChild(int groupPosition, int childPosititon) {
        return this.listDataHeader.get(groupPosition).getCh_list();
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        final DayBookDetailsData childText = listDataHeader.get(groupPosition).getCh_list().get(childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.item_day_book_data, null);
        }

        TextView item_txt_sub_cat_id = convertView.findViewById(R.id.item_txt_sub_cat_id);
        item_txt_sub_cat_id.setText(childText.getItem_id());

        TextView item_txt_sub_cat_name = convertView.findViewById(R.id.item_txt_sub_cat_name);
        item_txt_sub_cat_name.setText(childText.getItem_name());

        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this.listDataHeader.get(groupPosition).getCh_list().size();
    }

    @Override
    public DayBookData getGroup(int groupPosition) {
        return this.listDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this.listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        DayBookData headerTitle = getGroup(groupPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.item_day_book_header, null);
        }

        TextView item_txt_category_id = convertView.findViewById(R.id.item_txt_category_id);
        TextView item_txt_category_name = convertView.findViewById(R.id.item_txt_category_name);

        item_txt_category_id.setText(headerTitle.getCategory_id());
        item_txt_category_name.setText(headerTitle.getCategory_name());

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

您可以在侦听器下方绑定以获取并跟踪您对标题和子项的点击。

子项点击监听器

exp_listview.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

    @Override
    public boolean onChildClick(ExpandableListView expandableListView, View view, int groupPosition, int childPosition, long l) {

        DayBookDetailsData mItem = listData.get(groupPosition).getListInnerMenu().get(childPosition);

        // You can get your listView child click event and data here.

        return false;
    }
});

标题项目,点击“监听器”

exp_listview.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

    @Override
    public boolean onGroupClick(ExpandableListView expandableListView, View view, int position, long l) {

        DayBookData mItem = listData.get(position);

        // You can get your listView header click event and data here.

        return false;
    }
});

请尝试使用此代码。它可能对您有用。