在 Activity 之间共享光标

发布于 2024-10-03 18:27:54 字数 527 浏览 3 评论 0原文

我认为以下场景很常见,但我无法弄清楚实现它的方法:

我们有三个活动和一个数据库。我们要求用户输入信息以在数据库中进行搜索。该查询应该没有、一个或可能得到结果。

  • 如果没有收到,您​​只需通知用户即可。
  • 如果您只获得一个,则可以在新活动中以正确的视图显示它。
  • 如果你得到很多,你将它们显示在一个列表中,让用户选择正确的一个,然后将其传递给活动,该活动仅在一个结果的情况下显示数据。

问题是,为了知道结果的数量,您需要在第一个活动中执行查询(并获取游标)。如果您获得多个结果,您需要将数据(传递光标?)发送到列表活动。在列表活动中再次进行查询不可能是正确的,对吧?

我知道您可以通过使用内容提供程序来共享游标,但由于活动来自同一应用程序并且数据是私有的(在外部无用),因此没有必要将其提供给其他任何人。

我在这里读到,您可以创建一个可打包的光标并将其打包发送,但我不确定这是否是正确的用途。

关于如何解决这个问题有什么想法吗?

提前致谢。

I think the following scenario is common, but I can't figure the way to implement it:

We have three Activities, and one database. We ask the user for an input to search in the database. The query should get none, one or may results.

  • If you get none you simple inform the user.
  • If you get only one, you show it in a new activity with the right view.
  • If you get many you show them in a list to let the user to chose the right one and then you pass that to the activity which with show the data in the case of one result only.

The problem is that, in order to know the number of results, you need to do the query (and get the cursor) in the first activity. And in the case you get more than one result you need to send the data (pass the cursor?)to the list acitivty. Doing the query again in the list activity can't be right, right?

I'm aware that you can share cursors by using a content provider, but as the activities are from the same application and the data is private (useless outside), don't see the point of making it avaliable to anyone else.

I read here around that you can crate a parcelable cursor and send it in bundle, but I'm not sure if that is the right use.

Any idea on how to address this?

Thanks in advance.

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

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

发布评论

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

评论(1

影子的影子 2024-10-10 18:27:54

选项#1:搜索活动执行的查询只是SELECT _ID FROM... 在“none”情况下,它会显示消息。在“one”情况下,它将匹配的 _ID 传递给详细信息活动,以通过查询获取该行的所有所需列来查看匹配项。在“许多”情况下,它将搜索词传递给列表活动,该活动运行完整查询(包括显示列表所需的所有列)。

选项#2:将搜索活动和列表活动合并为一个活动。基本上,将“搜索并选择要查看的项目”视为“UI 事务”,并在一个活动中完成所有操作。搜索活动将执行足以在“许多”情况下填充列表的查询,通过 ViewFlipper 或其他方式在 AlertDialog 中或在主活动本身中显示列表。在“无”的情况下,它会显示该消息。在“一个”情况下——或者当用户点击列表中的条目时——它将内容传递到详细活动以查看该项目。

选项#3:将您的搜索查询移至列表活动 - 您的搜索活动通过额外功能将搜索信息传递到列表活动,这会执行 onCreate() 中的 rawQuery()代码>.列表活动处理“无”和“许多”情况。在“one”情况下,它仅对详细活动调用 startActivity()finish(),以便当用户按 BACK 时控制权返回到搜索活动。

在其他条件相同的情况下,我可能会选择选项#2。

Option #1: The query performed by the search activity is just SELECT _ID FROM... In the "none" case, it displays the message. In the "one" case, it passes the matching _ID to the detail activity to view the match by querying to get all needed columns for that one row. In the "many" case, it passes the search terms to the list activity, which runs the full query (including all columns needed to display the list).

Option #2: Merge the search activity and the list activity into one activity. Basically, consider "search and choose an item to view" as being a "UI transaction" and do that all within the one activity. The search activity would do a query sufficient to populate the list in the "many" case, displaying the list in an AlertDialog or in the main activity itself via a ViewFlipper or something. In the "none" case, it displays the message. In the "one" case -- or when the user taps an entry in the list -- it passes stuff to the detail activity to view the item.

Option #3: Move your search query to the list activity -- your search activity passes the search info to the list activity via extras, which does the rawQuery() in onCreate(). The list activity handles the "none" and "many" cases. In the "one" case, it just calls startActivity() on the detail activity and finish() so control returns to the search activity when the user presses BACK.

All else being equal, I'd probably go with option #2.

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