使用 SQLite 和服务器数据填充 ListVIew
我正在开发一个允许用户发送和接收消息的应用程序。这些消息存储在服务器上的数据库中。我想在 ListView
中显示消息,类似于电子邮件收件箱。这是我计划的实现:
- 启动消息活动时,使用
CursorLoader
和设备上存储的消息的SQLite
缓存填充列表(例如来自上周的消息) - 下载来自服务器的新消息并将它们添加到 SQLite 数据库,并更新光标
- 当用户滚动时下载旧消息(但不存储它们),并确保已加载足够多的消息,滚动无法加载滞后。
我的问题是:
- 总的来说,这似乎是一个好的策略吗?
- 当我下载并存储新消息时,是否可以更新
Cursor
使其数据与SQLite
数据库同步? - 当我下载旧消息时,有没有办法将它们添加到
Cursor
中,以便我可以让CursorAdapter
处理ListView
?
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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这听起来是一个不错的策略。这里的关键点是将两个数据源(SQLite 和 Server Data)包装成单个源并从中获取数据。
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.
我不确定更新光标是什么意思。
这是我的两分钱。
创建一个更新数据库的服务/线程。
既然您说过与邮件类似,这可以帮助您在新消息到达通知区域时通知用户。
然后每次用户从数据库启动活动查询时。
您可以使用时间戳或 messageId 来避免冲突。
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.