简体   繁体   English

android notifyDataSetChanged无法正常工作

[英]android notifyDataSetChanged doesn't work

I bind an adapter to ListView in onCreate(), every time when activity is in onResume(), I will update the appinfos data in the adapter, and called notifyDataSetChanged in adapter setdata() method. 我将适配器绑定到onCreate()中的ListView上,每次活动在onResume()中时,我将更新适配器中的appinfos数据,并在适配器setdata()方法中调用notifyDataSetChanged。 But I don't get The ListView refreshed even the data changed in my adapter. 但是,即使适配器中的数据更改,我也无法刷新ListView。 I can't figure out what's the problem, could you help me out? 我不知道出了什么问题,您能帮我吗? thanks 谢谢

Here is my activity using adapter and set data to listview. 这是我使用适配器并将数据设置为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;
}

Below is the main code of my UnistallListAdapter, and how to change its data. 以下是我的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();
}

please give me some help, and any hint about reason is welcome. 请给我一些帮助,欢迎提供任何有关原因的提示。 I would be highly appreciate your help. 非常感谢您的帮助。

Change this: 更改此:

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

to

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

reset the data contained in the list NOT its reference. 重置列表中包含的数据,而不重置其引用。

holder.ai = infos.get(position);

You're storing an object reference in the view holder and never update that. 您将对象引用存储在视图持有者中,并且永远不会更新它。

View holder is not supposed to contain references to data objects, just the views. 视图持有者不应该只包含对数据对象的引用。 Use getItem(position) to get an up-to-date data object each time in getView() . 每次在getView()使用getItem(position)获取最新的数据对象。

In the onResume method you are making a call to getAppInfos();. 在onResume方法中,您正在调用getAppInfos();。 However you are not assigning it your ArrayList appinfos. 但是,您没有为其分配ArrayList appinfos。 Hence it is not getting updated and passing the same list to setdata() method. 因此,它不会被更新并将相同的列表传递给setdata()方法。

Do, 做,

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

OR 要么

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

add applistView.setAdapter(adapter); 添加applistView.setAdapter(adapter); in the onResume after adapter.setdata(appinfos); 在adapter.setdata(appinfos)之后的onResume中;

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

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