使用 SQLite 和服务器数据填充 ListVIew

发布于 2024-11-27 12:32:59 字数 551 浏览 2 评论 0原文

我正在开发一个允许用户发送和接收消息的应用程序。这些消息存储在服务器上的数据库中。我想在 ListView 中显示消息,类似于电子邮件收件箱。这是我计划的实现:

  • 启动消息活动时,使用 CursorLoader 和设备上存储的消息的 SQLite 缓存填充列表(例如来自上周的消息)
  • 下载来自服务器的新消息并将它们添加到 SQLite 数据库,并更新光标
  • 当用户滚动时下载旧消息(但不存储它们),并确保已加载足够多的消息,滚动无法加载滞后。

我的问题是:

  1. 总的来说,这似乎是一个好的策略吗?
  2. 当我下载并存储新消息时,是否可以更新 Cursor 使其数据与 SQLite 数据库同步?
  3. 当我下载旧消息时,有没有办法将它们添加到 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 an SQLite 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 the Cursor
  • 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:

  1. Does this seem like a good strategy in general?
  2. When I download and store new messages, is there a way to update the Cursor so its data is synced with the SQLite database?
  3. When I download old messages, is there a way I can add them to the Cursor so that I can have a CursorAdapter handling the ListView?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

熊抱啵儿 2024-12-04 12:32:59

是的,这听起来是一个不错的策略。这里的关键点是将两个数据源(SQLite 和 Server Data)包装成单个源并从中获取数据。

  1. 是的,这听起来是一个不错的策略。
  2. 当您修改底层数据集时,您应该调用 notifyDataSetChanged 这将强制适配器重新获取其数据。
  3. 正如我所说,这里的关键是拥有适配器将从中获取数据的单一数据源结构。这里最好的解决方案是使用临时表。基本上临时表仅存在于与数据库的单个连接的生命周期中,因此当您关闭连接时该表将自动删除。当您打开数据库时,创建临时表并将所有消息插入其中。下载新消息时,将它们插入实际表和临时表中,但下载旧消息时,仅将它们插入临时表中。最后仅使用临时表来获取数据。

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.

  1. Yes is sounds like a good strategy.
  2. When you modify the underlying data set you should call notifyDataSetChanged which will force the adapter to refetch it's data.
  3. 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.
夏了南城 2024-12-04 12:32:59

我不确定更新光标是什么意思。

这是我的两分钱。

创建一个更新数据库的服务/线程。
既然您说过与邮件类似,这可以帮助您在新消息到达通知区域时通知用户。

然后每次用户从数据库启动活动查询时。

您可以使用时间戳或 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文