【问题标题】:android notifyDataSetChanged doesn't workandroid notifyDataSetChanged 不起作用
【发布时间】:2014-02-26 17:44:41
【问题描述】:

我在onCreate()中给ListView绑定了一个adapter,每次在onResume()中activity时,我都会更新adapter中的appinfos数据,并在adapter setdata()方法中调用notifyDataSetChanged。但是即使我的适配器中的数据发生了变化,我也没有刷新 ListView。我不知道是什么问题,你能帮帮我吗?谢谢

这是我使用适配器并将数据设置为列表视图的活动。

List<AppInfo> appinfos = new ArrayList<AppInfo>();
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getAppInfos();
    adapter=new UnistallListAdapter(this,appinfos);
    applistView.setAdapter(adapter);
}

@Override
protected void onResume() {
    getAppInfos();
    adapter.setdata(appinfos);
    super.onResume();
}


private List<AppInfo> getAppInfos() {
    appinfos.clear();
            //do some thing, new an item; and add into appinfos object
            //the appinfos data often changed here, 
            // as I uninstall app before activity called onResume().
           ......       
          appinfos.add(info);
            .....
    return appinfos;
}

下面是我的 UnistallListAdapter 的主要代码,以及如何更改它的数据。

private List<AppInfo>infos;
    public UnistallListAdapter(Context c,List<AppInfo>info)
{
    mContext = c;
    inflate=LayoutInflater.from(c);
    infos=info;
}

   @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return infos.get(position);
    }

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return infos.size();
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    ViewHold holder = null;
    if(convertView==null)
    {
        convertView=inflate.inflate(R.layout.uninstall_item, null);
        holder=new ViewHold();
        holder.name=(TextView)convertView.findViewById(R.id.name);
        holder.icon=(ImageView)convertView.findViewById(R.id.icon);

        holder.ai = infos.get(position);;
        convertView.setTag(holder);
    }
    else
    {
        holder=(ViewHold)convertView.getTag();
    }

    AppInfo ai = holder.ai;
    holder.name.setText(ai.mAppLabel);
    holder.icon.setImageDrawable(ai.mIcon);

    return convertView;
}

private class ViewHold
{
    AppInfo ai;
    ImageView icon;
    TextView name;
}
public void setdata(List<AppInfo> dataList) {
    infos = dataList;
    notifyDataSetChanged();
}

请给我一些帮助,欢迎任何关于原因的提示。非常感谢您的帮助。

【问题讨论】:

  • 请发布更多您的适配器代码。
  • 我认为如果我们专注于更改的数据就足够了。你想看哪一部分?另一部分代码与实现getView、getItem、getCount等流程是一样的……
  • 为什么在onResume()中调用update的东西?
  • @pskink,我在 onResume()->setdata()->notifyDataSetChanged(update stuff method) 中调用了 update stuff,我明白你的意思了吗?
  • @gladman 我在问为什么 onResume

标签: android listview android-listview


【解决方案1】:
holder.ai = infos.get(position);

您将对象引用存储在视图持有者中并且永远不会更新它。

视图持有者不应该包含对数据对象的引用,而只是视图。每次在getView()中使用getItem(position)获取最新的数据对象。

【讨论】:

  • 谢谢@laalto,我认为你是对的。你说的我试过了,刷新成功。我非常感谢您的帮助。
【解决方案2】:

改变这个:

public void setdata(List<AppInfo> dataList) {
  infos = dataList;
  notifyDataSetChanged();
}

public void setdata(List<AppInfo> dataList) {
  infos.clear();
  infos.putAll(dataList);
  notifyDataSetChanged();
}

重置列表中包含的数据而不是其引用。

【讨论】:

  • 是的,我试过你的回答,把putAll()换成addAll()吧?但结果是我在屏幕上看到了空白列表视图。我添加了调试日志,我注销了 dataList.size() 和 infos.size(),所有计数都为零。但在 onResume() 中,appinfos 大小为 13。 D/appsviewer(18022): onResume 13 D/appsviewer(18022): setdata count 0 dataList 0
【解决方案3】:

在 onResume 方法中,您正在调用 getAppInfos();。但是,您没有将 ArrayList appinfos 分配给它。因此它没有得到更新并将相同的列表传递给 setdata() 方法。

做,

this.appinfos = getAppInfos();
adapter.setdata(appinfos);
super.onResume();

adapter.setdata(getAppInfos());
super.onResume();

【讨论】:

  • 我理解它是一个成员变量的代码并且它的引用已经传递给了适配器。因此修改列表会修改已传递给适配器的相同列表。
  • 这不是真的,项目被添加到getAppInfos()方法内部的ArrayList成员中。
【解决方案4】:

添加 applistView.setAdapter(适配器);在adapter.setdata(appinfos)之后的onResume;

【讨论】:

    猜你喜欢
    • 2016-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 2014-03-03
    相关资源
    最近更新 更多