Android change data in adapter

杀马特。学长 韩版系。学妹 提交于 2019-12-06 11:03:07
hardartcore

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

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

That's all.

Short and fast, call these two lines after you feel changes have been done in dataset.

AppAdapter adapter=new AppAdapter(getPackageManager(), getResolveInfoList(0));
setListAdapter(adapter);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!