views:

1340

answers:

3

In my ListView, I choose an Item to "Add Favorite" and I have to do some stuff work, I fill data again to refresh the List, then I implement OnScrollListener use setSelection(firstVisibleItem) to set List display right on Item was added.

Are there any better way to do that. I mean like mCursor.requery() or something like that in android APIs ? Or any suggest way?

+1  A: 

Well, requery() is the preferred way to repopulate a ListView that is backed by a SimpleCursorAdapter. Calling setSelection() on the ListView seems like a fine way to ensure the desired item is visible. I am unclear what the OnScrollListener has to do with any of this.

CommonsWare
I had to think about the OnScrollListener, too! :-)But I **guess** what's meant here is that by using this listener and remembering the last value that came through `firstVisibleItem` and using **this** value when repositioning, everything looks identical after updating the data!
Zordid
A: 

If you just call setSelection(), that will make your item display in unnaturally. For example, If your item is visible at the 'middle' of the screen and its position is 8. When you add favorite, refresh data, setSelection(8) the Item will display on 'top' of the screen (rather it is in the 'middle').

So, that why I use OnScrollListener to get firstVisibleItem and use this value to set position. It makes the item display naturally. This is the function:

public void onScroll(AbsListView view, int firstVisibleItem, int VisibleItemCount, int totalItemCount){}

I think It happens frequently with anyone who updates data in ListView. And I hope someone can share experience in this problem.

Dennie
A: 

Oh Carl, thank you so much, this was troubling me hard before I read your excellent advise! firstVisibleItem rules!!!

Alexey
Next time please post this as a comment on the answer, not as a new answer.
I82Much