views:

128

answers:

1

Hi all

My application show a ListView with a button which allow user to add an element. When the user clicks on this button, another Activity is started to allow user to populate the new element. When the add is finished, we return to the previous Activity with the ListView and I would like to scroll to the new element. Note that this element is not necessarily at the end of the ListView because there is an "order by" when I retrieve the datas from the database.

I know I need the cursor position of the new element to make the ListView scrool to it, but the only info I have about this element is its id, so how to convert this id to cursor position ? Do I have to loop on the cursor to find the position ?

Thanks in advance

+1  A: 

If I had this right your activity is returning the new elements and than you fetch again all entries from database and the only info you have is the id of the elements. If this is the scenario i think that the only option is to iterate all elements in the adapter and to find the right one. But in this case maybe it's better to use ArrayAdapter. When the second activity returns the new ids, fetch only them from db and then add them to the adapter. You can override add(T) method by implementing insert sort, so your adapter will be sorted and you can store the index of newly inserted elements and then just scroll to this position.

Mojo Risin
Yes you are right, I finally choosed to iterate the cursor to find the position of the element, as you propose here. Then I call `getListView().setSelection( position )`. Thanks
kosokund