开发者

How to scroll to the new added item in a ListView

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开发者_StackOverflow中文版 ?

Thanks in advance


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜