I'm working on an application that allows users to send and receive messages. The messages are stored in a database on a server. I would like to display the messages in a ListView
, similar to an email inbox. Here is my planned implementation:
- On starting the message activity, populate the list using a
CursorLoader
and anSQLite
cache of messages stored on the device (e.g. from the last week) - Download new messages from the server and add them to the
SQLite
database, and update theCursor
- Download older messages as the user scrolls (but don't store them), and make sure there are enough messages loaded that scrolling doesn't lag.
Here are my questions:
- Does this seem like a good strategy in general?
- When I download and store new messages, is there a way to update the
Cursor
so its data is synced with theSQLite
database? - 开发者_运维知识库When I download old messages, is there a way I can add them to the
Cursor
so that I can have aCursorAdapter
handling theListView
?
Yes this sounds like a good strategy. The key point here is to wrap the two data sources (SQLite and Server Data) into single source and to fetch data from it.
- Yes is sounds like a good strategy.
- When you modify the underlying data set you should call notifyDataSetChanged which will force the adapter to refetch it's data.
- As i said the key here is to have single data source structure from which the adapter will fetch data. The best solution here will be to use temporary table. Basically temporary table exist only in the life of a single connection to the database, so when you close your connection the table will be dropped automatically. When you open the database create temp table and insert all messages in it. When downloading new messages insert them in the real table and in the temp table but when you download old messages insert them only in the temp table. And finally use only the temp table to fetch data.
im not sure what you mean by updating a cursor.
here is my two cents.
create a service/ thread which updates you data base. since you said similar to mail this can help you notify the use as new messages arrive in the notification area.
then every time the user starts the activity query from the data base.
you can use time stamp or messageId to avoid conflicts.
精彩评论