如何我在一个界面上有两个listview,当触发某项操作,使数据库数据发生变化,两个listview里面的数据都需要更新一下,应该如何刷新呢?
大家讲道理2017-04-17 16:23:24
Add a listener. The implementation of the function is to execute notifyDataSetChanged of two listview adapters
Write a function to monitor database changes. When receiving database changes, execute the listener interface function
PHPz2017-04-17 16:23:24
After you add the list to the adapter, ListView set adapter, when the data source list changes, calling the adapter's notifyDataChange method should refresh it~
天蓬老师2017-04-17 16:23:24
Use eventbus to distribute events, and then call the adapter’s notifyDataSetChanged() method
PHP中文网2017-04-17 16:23:24
If I understand "a certain operation causes data changes" correctly, you can use the observer mode to handle it, because one operation triggers another operation, and it is relatively simple and worry-free.
First register data monitoring:
getContentResolver().registerContentObserver(Uri uri, boolean notifyForDescendents, ContentObserver observer) ;
ContentObserver accepts callbacks after changes occur. You can put the code to update the ListView here:
private ContentObserver mObserver = new ContentObserver(new Handler()) {
@Override
public void onChange(boolean selfChange) {
// 重新查询数据
...
// 更新ListView1
listView1.notifyDataSetChanged();
// 更新ListView2
listView2.notifyDataSetChanged();
}
}
Notify ListView to update data:
getContentResolver().notifyChange(Uri uri, null, false);
The above is just an explanation of the idea. The actual implementation is up to you. You can also refer to my project
PS: Thanks @dabaooline for the reminder