gpt4 book ai didi

android - listView.setSelection(n); m=listView.getSelectedItemPosition();//m != n.为什么?

转载 作者:行者123 更新时间:2023-11-29 00:47:39 34 4
gpt4 key购买 nike

调用ListView的setSelection()好像有问题。很多人问这个问题。有答案,但没有一个有效。忽略视觉影响的问题。这是导致意外结果的基本场景:

listView.setSelection(5); //listView is a ListView. there are >= 6 items in the list
int sel=listView.getSelectedItemPosition();

你会期望 sel==5 但实际上它是 -1(哪个方法不起作用?)
那么这是一个错误吗?如果不是,管理所选项目的设置和检索的规则是什么?

最佳答案

如果您在 setSelection 的文档中看到,您会发现:

Sets the currently selected item. If in touch mode, the item will not be selected but it will still be positioned appropriately. If the specified selection position is less than 0, then the item at position 0 will be selected.

这样一来,它返回 -1 就完全合理了。即使您在触摸模式下运行此方法,也不会选择该项目,这很可能是您的情况。

关于android - listView.setSelection(n); m=listView.getSelectedItemPosition();//m != n.为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5456593/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com