gpt4 book ai didi

android - 在 Android 上使用 gridview 多次调用 getChildView

转载 作者:行者123 更新时间:2023-11-29 17:38:28 24 4
gpt4 key购买 nike

我有 2 组来自 API 的数据,每组都有 7 个对象,我试图在 GridView 中显示这些对象,因此我有一个可扩展的 ListView ,其中包含 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
}
}
...
}

关于android - 在 Android 上使用 gridview 多次调用 getChildView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29686842/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com