在Android上使用gridview多次调用getChildView

时间:2015-04-16 22:07:41

标签: android expandablelistview android-gridview expandablelistadapter

我有两组来自API的数据,每组有7个对象,我试图在gridview中显示,所以我有一个可扩展的列表视图,其中包含2个组,每个组有7个对象。问题是,当我运行应用程序时,2组显示的是最后一组的相同数据数组,因此2组正在打印第2组的相同7个对象。运行日志我看到getChildView被多次调用,我是android开发的新手,我不知道为什么会这样。有人能帮我吗?

public class ExpandListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private ArrayList<Group> groups;
    private ArrayList<ArrayList<Child>> child = new ArrayList();

    public ExpandListAdapter(Context context, ArrayList<Group> groups) {
        this.context = context;
        this.groups = groups;
        child = new ArrayList();
        for (int i = 0; i < groups.size(); i++) {
            child.add(i, groups.get(i).getItems());
        }
    }


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

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

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


        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.gridview, null);
        }
        CustomGridView gridView = (CustomGridView) convertView
                .findViewById(R.id.GridView_toolbar);

        for (int i = 0; i < groups.size(); i++) {

            ArrayList<String> listchild = new ArrayList<String>();

            for (int j = 0; j < groups.get(i).getItems().size(); j++) {

                listchild.add(child.get(i).get(j).getName());

            }
            gridView.setExpanded(true);
            GridAdapter adapter = new GridAdapter(context, listchild);
            gridView.setAdapter(adapter);// Adapter
        }

        return convertView;

    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        Group group = (Group) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater inf = (LayoutInflater) context
                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = inf.inflate(R.layout.group_item, null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.group_name);
        tv.setText(group.getName());
        return convertView;
    }

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

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

}

2 个答案:

答案 0 :(得分:3)

将根据每个组的项目数来调用getChildView 具有。因此,如果您的群组大小为2且每组群组有7个项目,那么应该总共有14个呼叫。你的getChildrenCount函数应如下所示:

public int getChildrenCount(int nGroup)
{
  return groups.get(nGroup).size();
}

你的getChildView还应检查当前处理的int groupPosition组。

  public View getChildView(int groupPosition, int nChild, boolean isLastChild, View convertView, ViewGroup parent)
  {
    try
    {
      if (groupPosition == 0)
      {
        ...
        ... your code here
      }
    }
    ...
  }

答案 1 :(得分:0)

这就是我所需要的,这样我就可以从各自的小组中获得所有项目:

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


        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.gridview, null);
        }
        CustomGridView gridView = (CustomGridView) convertView
                .findViewById(R.id.GridView_toolbar);


            listchild = new ArrayList<Child>();

            for(int j = 0; j< groups.get(groupPosition).getItems().size(); j++) {

                listchild.add(child.get(groupPosition).get(j));

            }

            gridView.setExpanded(true);
            GridAdapter adapter = new GridAdapter(context, listchild);
            gridView.setAdapter(adapter);// Adapter


        return convertView;

    }
            gridView.setExpanded(true);
            GridAdapter adapter = new GridAdapter(context, listchild);
            gridView.setAdapter(adapter);// Adapter