2

我将此适配器用于我的 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 类中使​​用此代码填充我的视图:

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

现在我已经更改了我称为 notifyDataSetChanged() 的列表应用程序,但我认为没有任何改变。请告诉我一个解决方案。谢谢

4

2 回答 2

8

这些信息仍然不足以看到问题并帮助您。这就是为什么我将用一个简单的例子来解释如何使用notifyDataSetChanged();on MyCustomAdapter。这是一个如何填充适配器的示例:

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

就这样。

于 2013-02-02T12:54:12.487 回答
1

简短而快速,在您感觉数据集中已完成更改后调用这两行代码。

AppAdapter adapter=new AppAdapter(getPackageManager(), getResolveInfoList(0));
setListAdapter(adapter);
于 2013-02-02T12:52:20.933 回答