Why does cursorLoader not notify about changes in the source data?

I have a simple contentProvider, a layout with a ListView, and a button for adding items to the Content Provider and CursorLoader. http://developer.android.com/reference/android/app/LoaderManager.LoaderCallbacks.html#onLoadFinished(android.content.Loader , D) the link indicates that

The bootloader will monitor data changes and notify you through new calls here. You do not have to control the data yourself. For example, if the data is a cursor and you place it in the CursorAdapter, use the CursorAdapter (android.content.Context, android.database.Cursor, int) without passing to FLAG_AUTO_REQUERY or FLAG_REGISTER_CONTENT_OBSERVER (i.e. use 0 for the flags argument). This prevents the CursorAdapter from executing its own Cursor observation, which is not required, since a change will occur, you will get a new Cursor that will cause another call.

But the Log.info line in the onLoadFinished method was not executed, and the listView was not updated. Here is my (simple) code:

public class HomeActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor>{ static final String TAG = "HomeActivity"; SimpleCursorAdapter adapter; ListView listAnnunciVicini; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.home); listAnnunciVicini = (ListView) findViewById(R.id.lista_annunci_vicini); adapter = new SimpleCursorAdapter(this, R.layout.list_item, null, new String[] { ContentDescriptor.Annunci.Cols.ID, ContentDescriptor.Annunci.Cols.TITOLO, ContentDescriptor.Annunci.Cols.DESCRIZIONE }, new int[] { R.id.list_annunci_item_id_annuncio, R.id.list_annunci_item_titolo_annuncio, R.id.list_annunci_item_descrizione_annuncio }, 0); listAnnunciVicini.setAdapter(adapter); // Prepare the loader. Either re-connect with an existing one, // or start a new one. getSupportLoaderManager().initLoader(0, null, this).forceLoad(); } public void addRandomItem(View sender) { ContentValues dataToAdd = new ContentValues(); dataToAdd.put(ContentDescriptor.Annunci.Cols.TITOLO, "Titolo"); dataToAdd.put(ContentDescriptor.Annunci.Cols.DESCRIZIONE, "Lorem ipsum dolor sit amet."); this.getContentResolver().insert(ContentDescriptor.Annunci.CONTENT_URI, dataToAdd); } @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { // creating a Cursor for the data being displayed. String[] proiezione = new String[] {ContentDescriptor.Annunci.Cols.ID, ContentDescriptor.Annunci.Cols.TITOLO, ContentDescriptor.Annunci.Cols.DESCRIZIONE }; CursorLoader cl = new CursorLoader(this, ContentDescriptor.Annunci.CONTENT_URI, proiezione, null, null, null); return cl; } public void onLoadFinished(Loader<Cursor> loader, Cursor data) { // Swap the new cursor in. (The framework will take care of closing the // old cursor once we return.) adapter.swapCursor(data); Log.i(TAG, "I dati sono stati ricaricati"); } public void onLoaderReset(Loader<Cursor> loader) { // This is called when the last Cursor provided to onLoadFinished() // above is about to be closed. We need to make sure we are no // longer using it. adapter.swapCursor(null); } } 

Any suggestion?

+7
source share
1 answer

AFAIK, you need to implement the notification yourself in ContentProvider . To do this, add something like getContext().getContentResolver().notifyChange(uri, null); in the insert , update and delete method of the ContentProvider and call .setNotificationUri(getContext().getContentResolver(), uri) on Cursor in query , as described in the official documentation . Here you can find the whole picture.

Note. . You must not close the cursor ( cursor.close() ) to receive notification of changes.

+20
source

All Articles