简体   繁体   English

Listview中的Gridview为每行重复最后一个gridview项

[英]Gridview in Listview is repeating the last gridview items for every line

I'm trying to make a listview with for each row: a title and grid of items. 我正在尝试为每一行创建一个列表视图:标题和项目网格。 With the below code I get a list of correct titles, but the gridView on each row is filled with the items of the last row. 通过下面的代码,我得到了正确标题的列表,但是每行的gridView充满了最后一行的内容。

The logging shows that every time getView is called it gets the correct items with the correct title. 日志记录表明,每次调用getView时,它都会获取具有正确标题的正确项。 I suspect there is a shared reference, or I'm writing to the wrong row somewhere. 我怀疑有共享的引用,或者我在某个地方写错了行。

Similar questions are all problems with the recycling of views, which I'm doing right. 类似的问题是视图回收中的所有问题,我做对了。

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = l_Inflater.inflate(R.layout.list_item_my_listview, null);
        holder = new ViewHolder();
        holder.txt_name = (TextView) convertView.findViewById(R.id.name);
        holder.myGridView = (GridView) convertView.findViewById(R.id.my_gridview);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    listItemDetails item = itemDetailsArrayList.get(position);
    holder.txt_name.setText(item.name);

    gridItemListAdapter gridArrayAdapter = new gridItemListAdapter(context, item.getGridItems());
    holder.myGridView.setAdapter(gridArrayAdapter);

    Util.Log(getClass().getName());
    Util.Log(cabinetId + " - " + cabinet.getCageList().size());
    return convertView;
}

Like I said in comment above, the problem occurs when you get the item's GridView on item.getGridItems() at this line: 就像我在上面的评论中说的那样,当您在此行的item.getGridItems()上获得项目的GridView时,就会发生问题:

gridItemListAdapter gridArrayAdapter = new gridItemListAdapter(context, item.getGridItems());

The problem is not the adapter which has each single instance. 问题不在于具有每个单个实例的适配器。 The last row's dup happens because - I'll try to guess your structure, I didn't know but I saw the clue -, each time you call this method, you might use the same array and so the same values. 最后一行的重复发生是因为-我将尝试猜测您的结构,我不知道,但我知道了线索-每次调用此方法时,您可能使用相同的array ,因此使用相同的值。
Thus, the last update for last row change every GridItems references in each adapter instances. 因此,最后一行的最后更新会更改每个适配器实例中的每个GridItems引用。

When I made my test to resolve this, I avoided to get it with an extra work in getView() in my adapters by calling an object treatment. 在进行测试以解决此问题时,我避免通过调用对象处理在适配器的getView()中进行额外的工作来获得它。 I used an object's field which was already setted. 我使用了已设置的对象字段。 So you have to do the same as you get item's name: item.name in order to get the gridview's items as follows: 因此,您必须执行与获取项目名称相同的操作: item.name ,以获取gridview的项目,如下所示:

gridItemListAdapter gridArrayAdapter = new gridItemListAdapter(context, item.gridItems);

And it should be like this in your Item object: 在您的Item对象中应该是这样的:

public ArrayList<Images> gridItems; // arraylist or whatever
...
public void setGridItems(ArrayList<Images> gridItems) { // set it when you set the name
    this.gridItems = gridItems;
}

Therefore, the Item object will have new GridItems for each instance. 因此, Item对象将为每个实例具有新的GridItems

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM