简体   繁体   中英

1st Activity's listview not updated after 2nd activity inserts into database via ContentProvider; onLoadFinished not called

Problem: upon returning from a 2nd activity the first activity's listview does not reflect database changes made by the 2nd activity. If I make a trivial change to code (to make emulator happy) and refire the app the listview now accurately shows the database changes the 2nd Activity made during the last session.

The guts of the app:

MainActivity: extends Activity implements LoaderManager.LoaderCallbacks<Cursor>
=>onCreate: SimpleCursorAdapter set; getLoaderManager().initLoader(LOADER_ID, null, this)
=>onResume: super.onResume(); getLoaderManager().restartLoader(0, null, this)

...oh my gawd! There's the problem: initLoader and restartLoader are using different loader ID's.

This stumped me for the last five hours. Why I am leaving this question: in trying to solve my problem I didn't run into this answer (maybe it's just too bone-headed of a thing to suggest someone look for) but for the next person who stumbles in this unique way, here's a potential answer.

Make sure LoaderManager ID's are the same in initLoader and restartLoader.

In addition to all the other posts by Alex and CommonsWare and all the rest of them that you will run into while searching for the solution to your failing-to-update listview, this is something else that might have happened to you.

During my searching for what could be wrong I also noted I had not implemented: cursor.setNotificationUri(getContext().getContentResolver(), uri);

( How do CursorLoader automatically updates the view even if the app is inactive? )

...two different tutorials I was following for how to implement content providers did not have that setContentNotificationUri in their providers' query methods; though they used a querybuilder and I did not (maybe setContentNotificationUri is included in the queryBuilder? Quick research didn't suggest that was the case).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM