GridView BaseAdapter notifyDataSetChanged不起作用

时间:2015-02-08 08:23:51

标签: android arraylist

成功添加了从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;
    }
    ....

1 个答案:

答案 0 :(得分:1)

我认为您忘了将updates添加到您的收藏中:

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);