在 Android 上使用 gridview 多次调用 getChildView

getChildView called multiple times using gridview on Android

我有 2 组数据来自 API,每组都有 7 个对象,我试图在网格视图中显示这些对象,所以我有一个可扩展的列表视图,其中包含 2 个组和 7 个对象为每一个。问题是,当我 运行 应用程序时,这 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;
    }

}

将根据每个组的项目数调用 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
      }
    }
    ...
  }

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

  @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