简体   繁体   中英

How to read the getChild in the ExpandaListView

I have an expandableListAdapter in

    public class ExpandableListAdapter extends BaseExpandableListAdapter

{
    private Context context;
    private List<String> listdataheader;
    private HashMap<String, Products> listdatachild;
    public ExpandableListAdapter(Context context,ArrayList<Products> fList)
    {
        HashMap<String, Products> listDataChild = new HashMap<String, Products>();
        for (Products data : fList) {
            listdatachild.put(data.Name, data);
        }

        List<String> listdataheader = new ArrayList<String>();
        for (Products data: fList) {
            listdataheader.add(data.Name);
        }

        this.context=context;
        this.listdataheader=listdataheader;
        this.listdatachild=listDataChild;
    }

I am having difficulty in rewriting the getChild, getChildrenCount methods. I am having the errors for the following methods

Cannot resolve method get('int') Cannot resolve method ('size')

 @Override
    public Object getChild(int groupPosition, int childPosition) {
        return this.listdatachild.get(this.listdataheader.get(groupPosition)).get(childPosition);
    }

and

@Override
public int getChildrenCount(int groupPosition) {
    // TODO Auto-generated method stub
    return this.listdatachild.get(this.listdataheader.get(groupPosition)).size();
}

I think you messed up with your nested get and size , keep it simple!

I would have done something like this:

 public class ExpandableListAdapter extends BaseExpandableListAdapter{
    private Context context;
    private List<String> listdataheader = new ArrayList<String>();
    private HashMap<String, Products> listdatachild = new HashMap<String, Products>();
    public ExpandableListAdapter(Context context,ArrayList<Products> fList)
    {
        for (Products data : fList) {
            listdatachild.put(data.Name, data);
        }

        for (Products data: fList) {
            listdataheader.add(data.Name);
        }

        this.context=context;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return listdatachild.get(this.listdataheader.get(groupPosition);
    }

    @Override
    public int getChildrenCount(int groupPosition) {
    return listdatachild.size();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM