onListItemClick() 中 position 和 row id 的实际区别 [英] Practical Difference Between position and row id in onListItemClick()

查看:18
本文介绍了onListItemClick() 中 position 和 row id 的实际区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 ListActivity.onListItemClick()postionrow id 之间的区别感到困惑.

I'm confused about the difference between postion and row id in ListActivity.onListItemClick().

文档内容如下:

position  The position of the view in the list
id        The row id of the item that was clicked

两者之间有什么实际区别吗?我假设,由于有两个不同的参数,存在值不同的情况,但根据文档中使用的措辞,很难看出差异可能是什么.是否其中之一可以保存由 Cursor 或其他一些特殊功能提供的数据库行 ID 的值?

Is there any practical difference between the two? I assume, since there are two different parameters, that there are situations where the values are different, but based on the wording used in the documentation, it's hard to see what that difference may be. Is it that one of them can hold the value of a database row ID provided by a Cursor or some other special capability?

推荐答案

position:视图在列表中的位置
id:被点击的item的行id

position: The position of the view in the list
id: The row id of the item that was clicked

位置从 0 开始,从 ListView 的顶部到底部,而要获得正确的行 id,重要的是传递给适配器的游标包含一个名为_id"的列,代表每一行的唯一 id在表中.

A position starts from 0, top to bottom of the ListView, whereas to get a proper row id it is important that the cursor, which was passed to the adapter, contains a column called '_id' representing unique id for each row in the table.

如果您使用的是 ArrayAdapter,positionid 变得相同.id 将是 Adapter.getItemId(int) 的返回值,因此如果您使用 ArrayAdapter,它将与位置相同 - 快速搜索 ArrayAdapter 源代码显示它正在使用返回位置;计算出 id.

If you are using an ArrayAdapter, position and id become the same. The id will be the returned value of Adapter.getItemId(int) so if you use an ArrayAdapter it will be the same as the position - a quick search of the ArrayAdapter source code shows it's using return position; to work out the id.

这篇关于onListItemClick() 中 position 和 row id 的实际区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆