Android更改适配器中的数据 [英] Android change data in adapter

查看:453
本文介绍了Android更改适配器中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将此适配器用于ListView:

I use this adapter for my ListView:

Appadapter extends ArrayAdapter<ResolveInfo> 
     private PackageManager pm=null;
     List<ResolveInfo> apps;
     AppAdapter(PackageManager pm, List<ResolveInfo> apps) {
    super(Launchalot.this, R.layout.row, apps);
  this.apps=apps;
  this.pm=pm;
}

@Override
public View getView(int position, View convertView,
                      ViewGroup parent) {
      Log.w(Launchalot.this.getPackageName(),"getView");
  if (convertView==null) {
    convertView=newView(parent);
  }

  bindView(position, convertView);

  return(convertView);
}

private View newView(ViewGroup parent) {
    Log.w(Launchalot.this.getPackageName(),"newView");
  return(getLayoutInflater().inflate(R.layout.row, parent, false));
}


private void bindView(int position, View row) {
  TextView label=(TextView)row.findViewById(R.id.label);
  Log.w(Launchalot.this.getPackageName(),"bindView");
  label.setText(getItem(position).loadLabel(pm));

  ImageView icon=(ImageView)row.findViewById(R.id.icon);

  icon.setImageDrawable(getItem(position).loadIcon(pm));
}
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
        setNotifyOnChange(true);}}

我在ListActivity类中使用以下代码填充视图:

I populate my view using this code in a ListActivity class:

AppAdapter adapter=new AppAdapter(getPackageManager(), getResolveInfoList(0));
setListAdapter(adapter);

现在我已更改了列表应用程序 我叫notifyDataSetChanged() 但是,正如我所认为的,什么都没有改变.请告诉我一个解决方案. 谢谢

Now i have changed List apps I've called notifyDataSetChanged() but, as I think, nothing changed. Please tell me a solution. Thanks

推荐答案

此信息仍然不足以解决问题并为您提供帮助.这就是为什么我将通过一个简单的示例来说明如何在MyCustomAdapter上使用notifyDataSetChanged();. 这是一个如何填充适配器的示例:

This information is still not enough to see the problem and help you. that's why I will explain with a simple example how to use notifyDataSetChanged(); on MyCustomAdapter. Here is a example how to populate the adapter:

private ArrayList<String> _names;
private MyCustomAdapter _adapter;
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    _names = new ArrayList<String>();

    for(int i = 0; i < _names.size();i++){
        _names.add("Element : "+i);
    }

    _adapter = new MyCustomAdapter(this, _names);
    _myListView.setAdapter(_adapter);


}

private void refreshListView(){
    _names.clear();
    for(int i = 0; i < _names.size();i++){
        _names.add("New Element : "+i);
    }
    _adapter.notifyDataSetChanged();
}

此处的想法是更改用于填充列表视图的List.清除它,用新数据填充它,然后调用notifyDataSetChanged();.

The idea here is to change the List which you are using to populate your listview. Clear it, populate it with the new data and after that call notifyDataSetChanged();.

仅此而已.

这篇关于Android更改适配器中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆