Bear in mind that this entire reply is pure guesswork, as I haven't 
experimented much with this facet of Androidology to date.

David Given wrote:
> My application is displaying a list of items from my database, in
> classic Android style, using a ListActivity and a SimpleCursorAdapter.
> So far, so good.
> 
> However, when I select my item, move to another Activity, and then press
> BACK and go back to my ListActivity, I find it's deselected its item and
> scrolled all the way to the top. The ListActivity hasn't been stopped or
> destroyed, merely frozen, so I'm surprised it's doing this.
> 
> Does anyone know why? 

One possibility is that you're using managedQuery(), the activity is 
making it to onStop() and then onStart(), and the act of the activity 
requerying the cursor has a side effect of resetting the list. That 
wouldn't surprise me much.

> And, more importantly, how can I stop it?

The following assumes the above theory is correct...

If you're sure your data set won't be a-changin' underneath you, you 
could try using an unmanaged cursor. However, if other activities have 
the capability of modifying the data you're showing in the list (e.g., 
it's a public content provider), then I suspect you *want* to requery 
upon the activity restarting, lest you show stale data to the user.

> On a related note, with a ListActivity, is it my responsibility to save
> the selected item in the icicle on freeze and restore it on thaw, or
> will the ListActivity do it for me?

Based on your experience, I'm betting its up to us to handle that, 
though ideally ListActivity would do it, at least for managed cursors.

 > If it is my job, does anyone know
> how I can determine the selected item position given a cursor ID?

Now that's a damn good question.

I think part of the problem is that the list may not have the item. I 
suspect -- though I have no proof -- that the ListView only requests 
views enough to fill the available screen real estate and doesn't ask 
for others until the user scrolls. Hence, there may not even be a 
position index for your cursor ID, if that object is not visible in the 
list. Not to mention the possibility that the selected row no longer 
exists, or has been modified to no longer match the query criteria, and 
as such won't ever be in the list and therefore is unselectable.

You could try subclassing ListView and play with the following 
interesting-sounding protected bits:

-- AdapterView#mSyncRowId is "Row id to look for when data has changed"

-- AdapterView#findSyncPosition() "searches the adapter for a position 
matching mSyncRowId..."

In theory, setting mSyncRowId to your cursor ID and then calling 
findSyncPosition() might give you the position index of the object 
represented by the cursor ID. Then, you'd call setSelection().

If that works, that's something begging to be put in a public API.

And, I'll repeat my opening caveat that everything I've said here is 
just a guess, your mileage may vary, past performance is no guarantee of 
future results, and so forth... ;-)

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com
Android Training on the Ranch in September! http://www.bignerdranch.com

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
Announcing the new M5 SDK!
http://android-developers.blogspot.com/2008/02/android-sdk-m5-rc14-now-available.html
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to