它是安全的游标传递给另一个活动? [英] Is it safe to pass a cursor to another activity?

查看:123
本文介绍了它是安全的游标传递给另一个活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用游标做水平刷卡,但我只是没有所著的Grokking的东西,希望这里有人能帮助我理解了。

I'm trying to do horizontal swiping using a cursor but I'm just not grokking something, and hopefully someone here can help me understand.

我已经得到了显示的项目列表的活动。它采用了的ListView 由一个自定义的的CursorAdapter 的支持,并且该适配器是由 LoaderCallback 。这是所有标准的东西。

I've got an activity that shows a list of items. It uses a ListView that is backed by a custom CursorAdapter, and that adapter is managed by a LoaderCallback. This is all standard stuff.

当用户在列表中点击一个项目,我想显示有关该项目的详细信息。目前,我开始一个新的活动,传递所选项目的ID。此活动将加载有关从数据库中的项目的信息。此外,标准的东西。

When the user clicks an item in the list, I want to show detailed information about that item. Currently I start a new activity, passing the ID of the selected item. This activity would load information about the item from a database. Again, standard stuff.

现在,来回的项目列表之间,然后点击一个项目以显示更多细节有关该项目是不是最好的用户体验,这将是preferable,允许用户点击一个项目显示它的信息,然后让刷卡显示有关列表中的下一个或previous项目细节。

Now, going back and forth between the list of items and clicking an item to show more detail about that item is not the best user experience, and it would be preferable to allow the user to click an item to display its information, then allow swiping to show detail about the next or previous item in the list.

所以我认为当点击在的ListView项目,我要开始一个包含活动 ViewPager 。这 ViewPager 由PagerAdapter,负责在适当的位置取意见支持。

So I assume that when an item is clicked in the ListView, I need to start an activity that contains a ViewPager. This ViewPager is backed by a PagerAdapter that is responsible for fetching views at the appropriate position.

所以我想通过的CursorAdapter 列表中的活动细节活动中使用,然后实施 PagerAdapter 遍历光标显示视图寻呼机当前项目的细节。这似乎很简单:的getItem(POS)会叫 cursor.moveToPosition(POS)然后从光标创建一个片段等等。

So I'd like to pass the CursorAdapter used in the list activity to the detail activity, and then implement a PagerAdapter that traverses the cursor to show the detail for the current item in the view pager. This seems simple enough: getItem(pos) would call cursor.moveToPosition(pos) then create a Fragment from the cursor, etc.

但(1)我不知道如何通过一个的CursorAdapter 来的活动,因为我不认为它可以被序列化的包内传球和做(2)我犹豫到一个CursorAdapter的传递到另一个反正活动,因为没有 LoaderCallbacks 连接到管理生命周期。

But (1) I'm not sure how to pass a CursorAdapter to an activity, as I don't think it can be serialized for passing within a bundle and (2) I'm hesitant to pass a CursorAdapter to another activity anyway because there's no LoaderCallbacks attached to manage the lifecycle.

一种可能的解决方案是,开始新的活性和有活动执行新的搜索,以获得等效的光标移动到列表中的活动中发现的光标。例如,清单活动可能表明匹配名称= foo的项目,所以项目10被点击时我们创建了一个新的细节活动,显示约10项信息,而且还执行称号= foo的搜索来获得光标可在水平刷卡使用。但是,这意味着我执行相同的搜索两次:在列表视图中的分页查看一次,一次。如果搜索需要一定的时间,这可能是一个问题。

One possible solution is to start the new activity and have that activity perform a new search to get an equivalent cursor to the cursor found in the list activity. For example, the list activity may have shown the items matching "title=foo", so when item 10 is clicked we create a new detail activity that shows information about item 10, but also performs a "title=foo" search to obtain a cursor that can be used in horizontal swiping. But that means I'm performing the same search twice: once in the list view, and once in the paging view. This may be an issue if the search takes some time.

我也可以在一些全局静态区域光标存储(如应用),但似乎充满了危险,以及

I could also store the cursor in some global static area (such as Application) but that seems fraught with peril as well.

还有 Parcelable 但我认为,这意味着我序列化的一面光标和反序列化它的另一面。对于大的列表也可能是更有效的,只是再次执行搜索以获得一个新的光标

There's also Parcelable but I assume this means that I am serializing the cursor on one side and deserializing it on the other side. For large lists it may be more efficient to just perform the search again to get a new cursor.

我敢肯定,这是一个常见的​​场景,所以我想知道什么是跨多个活动游标管理最佳实践,或者一些其他的方法可能会更好。

I'm sure this is a common scenario, so I'd like to know what is best practice for managing cursors across multiple activities, or if some other approach may be better.

推荐答案

没有。请不要传递任何光标或光标适配器或类似的东西其他活动。在这个过程中一定程度,你必须有传递活动的背景为对象的创作。如果该对象以某种方式制作成使用其他的上下文另一项活动,things'd可能会去疯狂。

No. please don't be passing any cursor or cursor adapters or anything like that to other activities. At some level of the process, you must have had to pass the activity's context for an object's creation. If that object somehow made into another activity using another's context, things'd probably go bonkers.

此外,我不明白为什么你需要适配器本身摆在首位。是不是那里,你可以只使用光标(重新实例)相同的光标为您的新活动,通过点击位置中,让你的 PagerAdapter 再一种方式pre-选择您要查看的详细位置?就像你说的什么,但你不需要一个新的搜索,传中意图位置

Moreover i don't see why you need the adapter itself in the first place. Isn't there a way that you could use just the cursor (re-instantiate) the same cursor for your new activity, pass the position clicked in and make your PagerAdapter and then pre-select the detail position that you want to view? just like what you said but you dont need a new search, pass the position in the intent.

这篇关于它是安全的游标传递给另一个活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆