0

Right now I am using CursorAdapter to populate my listview by getting the cursor data from my sqlite database. I am able to load the listview. I am able to add, edit or delete an item which also reflects in database. But when I do so the listview does not reflect it in the UI until i reload the app or activity.

I surfed a lot and found that I can achieve what I want using CursorLoader and LoaderManager. But I don't know that how to use them with CursorAdapter.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
kanudo
  • 2,119
  • 1
  • 17
  • 33
  • https://stackoverflow.com/questions/15517920/how-do-cursorloader-automatically-updates-the-view-even-if-the-app-is-inactive – akshay_shahane Jul 10 '17 at 12:42
  • just use adapter.notifydatasetchanged() – Hasmukh Kachhatiya Jul 10 '17 at 12:43
  • @Hasmukhkachhatiya The main thing is to requery the database and set the cursor with the updated data. – kanudo Jul 10 '17 at 12:56
  • ok sorry for my suggestion then its not good way to refresh adapter – Hasmukh Kachhatiya Jul 10 '17 at 12:58
  • `CursorLoader` delivers `Cursor` which you can use by simply calling `adapter.swapCursor(data)`, more https://developer.android.com/guide/components/loaders.html#example – pskink Jul 10 '17 at 13:17
  • @pskink then does i require to use CursorAdapter or just CursorLoader will work? – kanudo Jul 10 '17 at 13:19
  • both: loader loads `Cursor` in the background and adapter uses it to show it in your list view, all what you need is a "smart" content provider like this: https://android.googlesource.com/platform/development/+/05523fb0b48280a5364908b00768ec71edb847a2/samples/NotePad/src/com/example/android/notepad/NotePadProvider.java, notice `setNotificationUri` and `notifyChange` methods – pskink Jul 10 '17 at 13:21

0 Answers0