如何将标识符与 ListView 中的项目一起传递?
我有一个 ListView,其中包含数据库表 EMPLOYEE 中的 EMPLOYEE_NAME。有些员工可能有相同的名字,这意味着我可能在 ListView 中有重复的项目。
识别用户选择的项目(员工)的最佳解决方案是什么?我知道事件 onItemClick 返回与所选项目关联的视图,并执行 ((TextView) view).getText() 获取员工姓名。
但是将行 ID 与员工姓名一起传递的解决方案是什么?我应该扩展 TextView 以包含行 id 吗?
I have a ListView which contains EMPLOYEE_NAME from a DB table EMPLOYEE. Some employees may have the same name, which means I could have duplicated items in the ListView.
What would be the best solution for identifying which item (employee) the user selected? I know that the event onItemClick returns the View associated with the selected item and doing a ((TextView) view).getText() gets me the employee name.
But what would be the solution for passing the row id along with the employee name? Should I extend TextView to include the row id?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
onItemClick 包含一个名为 id 的长参数,这是您想要使用的:
因此,如果您使用
CursorAdapter
,您唯一要做的就是确保数据库中的 ID 行称为_id
。如果您使用其他类型的适配器,例如
BaseAdapter
,您必须重写getItemId
方法,您将在其中返回员工的 ID 。The onItemClick contains a long parameter called id, which is what you would want to use:
So, if you are using a
CursorAdapter
the only thing you have to do is making sure that the ID row in the database is called_id
.If you are using other kind of adapters, say
BaseAdapter
, you have to override thegetItemId
method, where you will return the ID of the employee.