如何滚动到 ListView 中新添加的项目

发布于 2024-08-31 21:21:51 字数 280 浏览 0 评论 0原文

我的应用程序显示一个带有按钮的 ListView,允许用户添加元素。当用户单击此按钮时,将启动另一个活动以允许用户填充新元素。添加完成后,我们使用 ListView 返回到上一个 Activity,我想滚动到新元素。 请注意,此元素不一定位于 ListView 的末尾,因为当我从数据库检索数据时有一个“order by”。

我知道我需要新元素的光标位置才能使 ListView 滚动到它,但我拥有的有关该元素的唯一信息是它的 id,那么如何将此 id 转换为光标位置?我是否必须循环光标才能找到位置?

提前致谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遗失的美好 2024-09-07 21:21:51

如果我有这个权利,您的活动将返回新元素,然后您再次从数据库中获取所有条目,您拥有的唯一信息就是元素的 id。如果是这种情况,我认为唯一的选择是迭代适配器中的所有元素并找到正确的元素。但在这种情况下,也许使用 ArrayAdapter 更好。当第二个活动返回新的 id 时,仅从数据库中获取它们,然后将它们添加到适配器中。您可以通过实现插入排序来覆盖 add(T) 方法,因此您的适配器将被排序,并且您可以存储新插入元素的索引,然后只需滚动到该位置。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文