2

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?

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
CompEng88
  • 1,336
  • 14
  • 25
  • will you tell me how did you store the chat messages. i created chap app and now want to store the chat message, but do not know what is best approach to store the message. i know the basic use of Sqlite. – Devendra Singh Nov 19 '15 at 21:08

1 Answers1

2

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

Fergal Moran
  • 4,525
  • 5
  • 39
  • 54
  • Thank you for that. I didn't even know about the compatibility library so I went and downloaded it and it contains those missing classes. :) -- I'll have to look up a few examples of how to do this best, but this is a step in the right direction. – CompEng88 Dec 05 '11 at 00:36
  • is getLoaderManager().initLoader(0, null, this) in a ListFragment which extends LoaderManager.LoaderCallbacks the correct way to do this? When I insert into the database is it supposed to detect the change and insert the view into the fragment for me? This is the kind of optimization I am looking for. Suggestions? – CompEng88 Dec 06 '11 at 19:59
  • Yes, that's exactly how it works. Have a look at this article, it told me everything I needed to know - http://developer.android.com/guide/topics/fundamentals/loaders.html – Fergal Moran Dec 07 '11 at 12:40
  • I have done this except for one thing that I had to change: I created a custom CursorLoader which queries a sqlite database instead of using a URI from a ContentRevolver. I basically took CursorLoader and changed loadInBackground() to simply returned the Cursor from a sqlite query. The only problem is, the interface DOES NOT update when a new item is inserted into the db. I have to call notifyDataSetChanged() on my CursorAdapter and register a DataSetObserver which calls getLoaderManager().restartLoader(..) in onChange(). This can't be optimal. What am I missing? – CompEng88 Dec 07 '11 at 23:13
  • Have you used FLAG_REGISTER_CONTENT_OBSERVER when setting up your CursorAdapter? – Fergal Moran Dec 11 '11 at 13:38
  • @FergalMoran I want to use LoaderMnager in my project that it's used on a 2.3.3 Android device. You've said that the LoaderManager is available for 2.3.3 whereas Google says that is Added in API level 11. API 11 is related to android 3. What should I do? – Behzad Dec 27 '12 at 22:49
  • 1
    Hi @Behzad - you can use the support library to get the LoaderManager in pre SDK 11 devices. http://developer.android.com/tools/extras/support-library.html – Fergal Moran Jan 01 '13 at 16:53