我有2组来自API的数据,每组都有7个对象,我正尝试在gridview中显示它们,所以我有一个可扩展的列表视图,其中有2组,每组7个对象。问题是,当我运行该应用程序时,两个组显示的是最后一个相同的数据数组,因此两个组正在打印第二组中相同的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
      }
    }
    ...
  }

09-28 10:14