GridView BaseAdapter notifyDataSetChanged 不工作

GridView BaseAdapter notifyDataSetChanged not working

成功添加了从 ArrayList 加载图像的功能后,当新 ArrayList 到达时我无法刷新显示。 notifyDataSetChanged() 似乎没有正常工作。我在寻找解决方案的同时看到了很多与此有关的问题,所有问题都不同,但似乎没有任何效果。相关代码如下:

final ImageAdapter myAdapter = new ImageAdapter(this,imageCollection); //---Initial onCreate call--
....
ImageAdapter adapter = new ImageAdapter(this,newTemp);
adapter.updateContent(newTemp);                                 //---Need to refresh display here--
....
public class ImageAdapter extends BaseAdapter {
    private Context context;
    private final int colCount;
    private ArrayList<String> anyCollection;

    public ImageAdapter(Context c, ArrayList<String> anyCollection) {
        super();
        this.context = c;
        this.anyCollection = anyCollection;
        this.colCount = anyCollection.size();               //--- 16 and then 3 (ArrayList sizes)--
        Log.d("DEBUG","Line 124 Size: " + this.colCount);   //--- Shows ArrayList size (Correct) --
    }

    public void updateContent (ArrayList<String> updates) {
        Log.d("DEBUG","Line 126 " + this.anyCollection.size());              //--- Shows 3 again --
        this.notifyDataSetChanged();                  //--- Screen still shows original 16 images--
    }

    //---Returns the number of images---
    public int getCount() {
        return colCount;
    }
    ....

我想你忘了将 updates 添加到你的 collection:

public void updateContent(ArrayList<String> updates) {
    anyCollaction.addAll(updates);
    Log.d("DEBUG","Line 126 " + this.anyCollection.size());              //--- Shows 3 again --
    this.notifyDataSetChanged();                  //--- Screen still shows original 16 images--
}

此外,您应该在添加新值时更新 colCount

您不应该在每次要更新内容时都创建新的 ImageAdapter 实例:

ImageAdapter adapter = new ImageAdapter(this, newTemp);
adapter.updateContent(newTemp);