Search code examples
androidsqlitexmppandroid-loadermanager

Android db loading chat for chat application


I'm creating a chat application and I need a little bit of guidance. I am using a sqlite database to store the chat as it comes in. I want my Activity, when it's open, to load the chat history from that particular chat and to continue to refresh as new chat is entered (my main question is how do I go about doing this).

Should I use a CursorAdapter with an inital cursor of a query containing the chat for that conversation and set it as the adapter for the ListView? I have tried this, but the data doesn't refresh when I insert into the database.

I know I didn't provide any code, but general conversation about what the best way is to go about this is welcome. Thanks!

Let me also mention that I need this to work for android 2.3.3 (API 10) and up (CursorLoader and all that is not available until API 11 which I did read a little about). The other thing I can do is use an ArrayAdapter and add chats directly into that (if the activity is open) and also insert it into the db in case it is not and then on onResume(), clear the ArrayAdapter and query all the convo once and readd each. Would this be the most optimal way?


Solution

  • Have a look at LoaderManager. Really simple and does exactly what you want. It is available for 2.3.3 using the compatibility library.

    http://developer.android.com/reference/android/app/LoaderManager.html