android获取id列表,Android:获取列表视图中项目的位置给定其ID:

不确定问题是否仍然存在。 尽管如此,我想我会分享我是如何做到的,以便它可以帮助那些想要做同样事情的人。

您可以使用视图的标记function来存储该视图的id与其在列表中的位置之间的映射。

android开发者网站上的标签文档很好地解释了这一点:

示例:在列表适配器的getView方法中,您可以设置该视图的id及其在列表中的位置的映射,例如:

public View getView (int position, View convertView, ViewGroup parent) { if(convertView == null) { // create your view by inflating a xml layout resource or instantiating a // custom view class } else { // Do whatever you want with the convertView object like // finding a child view that you want to set some property of,etc. } // Here you set the position of the view in the list as its tag convertView.setTag(convertView.getId(),position); return convertView; }

要检索视图的位置,您可以执行以下操作:

int getItemPosition(View view) { return view.getTag(view.getId()); }

需要注意的一点是,您需要引用要查找其位置的视图。 如何掌握View的参考取决于您的具体情况。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值