本文介绍了如何读取ExpandaListView的getChild的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在

的expandableListAdapter

 公共类ExpandableListAdapter扩展BaseExpandableListAdapter{
    私人上下文的背景下;
    私人列表<串GT; listdataheader;
    私人的HashMap<弦乐,产品> listdatachild;
    公共ExpandableListAdapter(上下文的背景下,ArrayList的<产品> FLIST)
    {
        HashMap的<弦乐,产品> listDataChild =新的HashMap<弦乐,产品>();
        对于(产品数据:FLIST){
            listdatachild.put(data.Name,数据);
        }        清单<串GT; listdataheader =新的ArrayList<串GT;();
        对于(产品数据:FLIST){
            listdataheader.add(data.Name);
        }        this.context =背景;
        this.listdataheader = listdataheader;
        this.listdatachild = listDataChild;
    }

我有在重写getChild,getChildrenCount方法的难度。
我有以下方法错误

 @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();
    }
}

这篇关于如何读取ExpandaListView的getChild的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 02:32