Android ListView Live Updates

I have a ListView in ListActivity associated with some data. I have a content provider that provides data.

ListActivity retrieves data by requesting an ad:

Uri uri = Uri.parse("content://my.provider.DocumentProvider/mystuff"); contentCursor = this.getContentResolver().query(uri, null, null, null, null); 

So now the activity has a cursor. It creates an adapter and attaches it to the list:

 ListAdapter adapter = new DocumentListCursorAdapter(this, R.layout.main_row_layout, contentCursor, new String[] { "titleColumn" }, new int[] { titleColumnIndex }); setListAdapter(adapter); 

This works great; the list shows the data in the cursor.

But now the content provider has new data. I want the list to be updated to show new data.

The examples I saw include calling the notifyDataSetChanged adapter, but it seems to me that this breaks the separation between the content provider and the list that consumes the content.

Does the content provider need to know which adapters are connected to the cursor so that it can call its notifyDataSetChanged method? Or is there a better way that these two things do not see this way.

+8
android listview cursor
source share
3 answers

I found the answer here:

http://mylifewithandroid.blogspot.com/2008/03/observing-content.html

In short, the provider calls notifyChange to indicate that the content in the URI has changed:

 getContext().getContentResolver().notifyChange(uri, null); 

And ListActivity calls setNotificationUri on the cursor to register that it is interested in receiving notification of changes:

 contentCursor.setNotificationUri(getContentResolver(), uri); 

(Thanks to njzk2 for pointing me in the right direction).

+6
source share

since the ContentProvider is yours, you must add cursor.setNotificationUri(getContext().getContentResolver(), uri); in the query CP implementation (before you return the cursor) and getContext().getContentResolver().notifyChange(uri, null); in update , insert , delete ... SimpleCursorAdapter which is the dense base of your DocumentListCursorAdapter , should take care of updating the list.

+1
source share

You mean that you want to update the list according to the changed data.
To do this, simply try the following:
when you get a new cursor, just set this code in place of the new adapter. List

 adapter.notifyDatasetChanged(); 

and

 listview.invalidate() 
0
source share

All Articles