I was wondering : I’m trying to make an app that as a Listview, displaying data from a database.
Here is the specific thing : I want that on a event that modified the database, the listview is automatically updated, as if there was a “listenner” on the database that … well automatically upload the listview.
From what I read, I should use a loadManager, a cursor, fragment (to allows me to use loadManager for anterior versions of 3.0) … but as I’m a beginner, it’s kind of hard to me to understand.
I was wondering if someone could just try to explain me deeply how to use those tools together (I’m not asking for code, just explanation 🙂 )
Thanks !
EDIT : Ok as I’m new and I’m very lost, I need some more explanation.
The Different steps are :
1) Activity create a listView, a custom CursorAdapter and a cursor, on the onCreate(). Then still on onCreate, it feed the cursor with a standart request on SQLiteDatabase object. Then we affect the cursor to the adapter, and the adapter to the listView.
2) When an insert event comes, I use an insert function of SQLiteDatabase and then get the result of the request on a cursor, and give it to the CursorAdapter of the list (will it automatically update the listview ?)
3) For my customAdapter well … The more I read sources, examples and tutorial, the more I get lost. Just understood that I have to override bindView() and newView(), I didn’t really get their purposes, plus the way I want to create many data type for my adapter…
I feel like I’m not really smart, but I really tried and I’m still lost 🙁
If you use a CursorAdapter on your ListView, that’s all done for you! http://developer.android.com/reference/android/widget/CursorAdapter.html
Edit: Sorry, I should explain more. I read it quick and I was thinking you had the ListView already and wanted to keep it updated.
When you get your data from the database, whether through ContentResolver.query, or SQLiteDatabase.query, or something else, you will get a Cursor back. You need to use this in a CursorAdapter.
You need to subclass CursorAdapter and override bindView to use the data from the cursor inside the ListView (since it doesn’t know what columns you have and what field maps to what view). Assuming you use the FLAG_REGISTER_CONTENT_OBSERVER flag when creating the CursorAdapter, every time the data in your cursor gets updated the list should update.