问题
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.
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.
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.
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.
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.
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.
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.
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.
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.
回答1:
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.
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.
来源:https://stackoverflow.com/questions/13774020/is-it-safe-to-pass-a-cursor-to-another-activity