Android: how to refresh ListView contents?

后端 未结 9 924
渐次进展
渐次进展 2020-11-28 04:58

My ListView is using an extension of BaseAdapter, I can not get it to refresh properly. When I refresh, it appears that the old data draws on top

相关标签:
9条回答
  • 2020-11-28 05:55

    You don't have to create a new adapter to update your ListView's contents. Simply store your Adapter in a field and update your list with the following code:

    mAdapter.setList(yourNewList);
    mAdapter.notifyDataSetChanged();
    

    To clarify that, your Activity should look like that:

    private YourAdapter mAdapter;
    
    protected void onCreate(...) {
    
        ...
    
        mAdapter = new YourAdapter(this);
        setListAdapter(mAdapter);
    
        updateData();
    }
    
    private void updateData() {
        List<Data> newData = getYourNewData();
        mAdapter.setList(yourNewList);
        mAdapter.notifyDataSetChanged();
    }
    
    0 讨论(0)
  • 2020-11-28 05:59

    I'm doing the same thing using invalidateViews() and that works for me. If you want it to invalidate immediately you could try calling postInvalidate after calling invalidateViews.

    0 讨论(0)
  • 2020-11-28 06:01

    Another easy way:

    //In your ListViewActivity:
    public void refreshListView() {
        listAdapter = new ListAdapter(this);
        setListAdapter(listAdapter);
    }
    
    0 讨论(0)
提交回复
热议问题