Android db loading chat for chat application

一个人想着一个人 提交于 2019-12-03 22:22:46

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/8379677/android-db-loading-chat-for-chat-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!