开发者

Android: Possible solution for updating the custom list view at random times

开发者 https://www.devze.com 2023-02-20 08:02 出处:网络
I am having a situation where I want to update my Custom List View using BaseAdapter whenever my Database is updated. I have tried calling invalidate() on this Custom List but it didn\'t work, similar

I am having a situation where I want to update my Custom List View using BaseAdapter whenever my Database is updated. I have tried calling invalidate() on this Custom List but it didn't work, similarly I even tried having a timer to update my list after sometime, that didn't work either. Please let me know of possible solution.

Update:

This is how I am making my custom list view

li= (ListView)findViewById(R.id.id_lv_row); 
ColorDrawable divcolor = new ColorDrawab开发者_运维知识库le(Color.DKGRAY); 
registerForContextMenu(li); 
li.setDivider(divcolor); 
li.setDividerHeight(2); 
li.setAdapter(new FriendsPositionAdapter(this));


BaseAdapter.notifyDataSetChanged() should do the trick as long as the data behind the adapter actually changed. That's all you need to do to refresh the list.


Invalidate is for repainting views only, you have to tell to the List adapter (BaseAdapter) that dataset has changed. When the data changes, asign the new dataset to the adapter, and later call notifyDataSetChanged()...

in order to make functional notifyDataSetChanged() the adapter data must be changed. Remember that the original data that change is not reflected automatically to the adapter.

//here i retrieve the new list, named "beans"
lista = (BeanList) result.getDataObject();
Vector<Bean>beans = list.getBeanList();
((BeanListAdapter)listAdapter).syncData(beans);
((BeanListAdapter)listAdapter).notifyDataSetChanged();

//now the syncData method
public void syncData( List<PINPropiedad> newData ){
    for(Object o : newData){
        add(o);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消