ListView 和隐藏的 Id。怎么可能呢?
我正在开发一个 Android 应用程序,现在我已经实现了一个 ListView,它显示连接到数据库的课程列表。
我想知道如何在名称中包含一个隐藏的 ID(来自数据库),以便一旦用户单击元素,应用程序就会转到所选课程的相对视图。
如何在课程视图内导航期间维护 id?
目前我的代码只是从数据库加载课程名称并在列表视图中设置:
ArrayAdapter<String> result = new ArrayAdapter<String>(CourseActivity.this, android.R.layout.simple_list_item_1);
for (CourseRecord c : get())
result.add(c.getFullname());
lv.setAdapter(result);
显然我也可以执行 c.getid() 但我不知道将 id 放在哪里。
非常感谢。
PS:也许有人也有一个非常漂亮的列表视图图形?
I'm developing an application for android and now I have implemented a ListView that shows a list of courses, connected to a database.
I would like to know how to include, with the name, an hidden id (that come from the db) so that once the user click on the elements the app goes to the relative view of the selected courses.
And how can I maintain the id during the navigation inside the course-view?
At the moment my code just load the name of the courses from the db and set in the list view:
ArrayAdapter<String> result = new ArrayAdapter<String>(CourseActivity.this, android.R.layout.simple_list_item_1);
for (CourseRecord c : get())
result.add(c.getFullname());
lv.setAdapter(result);
obviously I'm able to do also c.getid() but I don't where to put the id.
Thank you very much.
P.S.: Maybe does someone have also a really nice graphics of list view?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像这样改变你的阵列适配器。
并有一个项目单击处理程序来接收选定的 id
将侦听器分配给列表
change your array adapter like this.
and have an item click handler to recieve the selected ids
assign the listener to the list
您可以将 id 存储在隐藏的 TextView 中。在列表项 XML 中,将 'android:visibility="gone"' 添加到 TextView。同样,在点击处理程序中,您可以从文本视图中读取 id。
You can store the id in a hidden TextView. In the list item XML, add 'android:visibility="gone"' to the TextView. Likewise in the click handler you can read the id from the textview.
您还可以使用 View 的 setTag(Object object) 方法存储 id。使用 getTag() 方法从该视图中提取该 id。
You can also store id using setTag(Object object) method of a View. Use getTag() method to extract that id from that view.