有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java GridView在notifyDataSetChanged()之后变为空白

我设置了一个刷新按钮来更新我的gridview。 Fragment将获得新数据,并从Fragment->;回收视图->;GridView

public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.refresh:
                Log.v(Parameters.TAG,"refresh..");
                //Get new list
                setPhotos(newData)
                break;
        }
        return true;
    }

当RecycleServiceAdapter调用notifyDataSetChanged()时

private List albumPhotoItem = new ArrayList<>();
@Override
protected void onBindItemViewHolder(AlbumViewItemHolder holder, int section, int position) 
{
    
     String title = mTitle[section].toString();
     albumPhotoItem.addAll(newData.get(title));
     Log.v(Parameters.TAG,"albumPhotoItem photos hash code"+System.identityHashCode(albumPhotoItem));
       
     holder.setData(albumPhotoItem,title);

    }

在GridView中:

mGridView =  (GridView) itemView.findViewById(R.id.gv_album);
mAdapter = new MyAdapter();
mGridView.setAdapter(mAdapter);
public void setData(List photos,int colCount){

    mGridView.setNumColumns(colCount);
    mAdapter.setPhotos(photos,colCount);
}

GridViewAdapter代码:


class MyAdapter extends BaseAdapter
    {

        List<Map.Entry<String,List<AlbumPhotoItem>>> photos;
        private int colCount;
        public void setPhotos(List<Map.Entry<String,List<AlbumPhotoItem>>> photos,int colCount,int tag)
        {
            this.colCount = colCount;
            this.photos = photos;
            notifyDataSetChanged();
            Log.v(Parameters.TAG,"adapter photos hashcode:"+System.identityHashCode(this.photos));
            Log.v(Parameters.TAG,"origin photos hashcode:"+System.identityHashCode(photos));
            Log.v(Parameters.TAG,"adapter hashcode:"+System.identityHashCode(this));
            
            Log.v(Parameters.TAG,"photos hashcode:"+photos);
        }
        @Override
        public int getCount() {
            return Format.isEmpty(photos) ? 0 : photos.size();
        }
        ...
}

.setPhotos(photos,colCount,tag);

然后我得到了日志:

photo size:3
adapter photos hashcode:188616529
origin photos hashcode:188616529
adapter hashcode:147396791 
gridview hashcode129071140
******hashcode after refresh******
photo size:6
adapter photos hashcode:188616529
origin photos hashcode:188616529
adapter hashcode:147396791
gridview hashcode:129071140

当我按下按钮时,gridview变为空白

我使用相同的列表,但它不起作用

这个问题困扰了我很长时间╥﹏╥.

请帮帮我


共 (0) 个答案