分配隐藏值到ListView项目 [英] assigning hidden values to a listview item

查看:185
本文介绍了分配隐藏值到ListView项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个扩展的ListView的活动。我填充我有一个查询结果的SQLite数据库列表。列表元素组成的start_time,END_TIME,CLIENT_NAME和状态。以上是关于信息的4位,我需要显示每个列表项。

I have an activity that extends ListView. I populate my list with the results of a query to the sqlite database. A list element consists of start_time, end_time, client_name and status. These are the 4 bits on info I need to display for each list item.

我的问题是:是否有可能对我来说,分配隐藏元素此ListView项

My question is: Is it possible for me to assign hidden elements to this ListView item?

例如,我希望存储数据库行的_id领域的ListView项,这样,当我点击它基于该数据库的数据行我就可以开始一个新的活动。

For example, I want to store the _id field of the database row for that ListView item, so that when I click it I can start a new activity based on data for that database row.

修改

在code我用它来填补我的列表,我的光标:

The Code I use to fill my list from my Cursor:

String[] columns = new String[] {VisitsAdapter.KEY_CLIENT_FULL_NAME, VisitsAdapter.KEY_STATUS,VisitsAdapter.KEY_CLIENT_START_TIME, VisitsAdapter.KEY_CLIENT_END_TIME};

// the XML defined views which the data will be bound to
int[] to = new int[] { R.id.name_entry,R.id.number_entry,R.id.start_time_display,R.id.end_time_display  };

SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(HomeScreen.this, R.layout.list_element, cur, columns, to);

在哪里R.layout.list_item是XML我的列表项。我来自一个PHP / HTML的背景所以我通常会在这里做的是有一个隐藏字段来保存我的_id值,我可以再一次访问列表项被点击。我可以做Android的类似的东西?我可以把一个隐藏_id场在我的XML布局?

Where R.layout.list_item is the XML for my list item. I come from a PHP/HTML background so what I would typically do here is have a hidden field to hold my _id value that I could then access once the list item was clicked. can I do something similar in android? can I put a hidden _id field in my xml layout?

推荐答案

您应该尝试从心态一点点的Web开发框架放松自己。有没有需要存储这样的事情作为ID在一个隐藏字段在视图中。这会混淆你的数据与您的用户界面,并创造了很多混乱和不必要的视图对象。

You should try to loosen yourself from the web development frame of mind a little bit. There is no need to store such a thing as the id in a hidden field in your view. That would mix your data with your user interface and create a lot of confusion and unnecessary view objects.

如果你正在使用SQLite数据库来存储你的价值观,你应该使用的CursorAdapter 以经理列表项。 CursorAdapter的存储您从数据库获得并管理如何将数据显示给用户的结果。如果再注册一个<一个href="http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html">onItemClickListener在<一个href="http://developer.android.com/reference/android/widget/AdapterView.html#setOnItemClickListener%28android.widget.AdapterView.OnItemClickListener%29">ListView听者将接收适配器和其单元被点击在适配器中的数据项的位置。现在你可以使用<一个href="http://developer.android.com/reference/android/widget/CursorAdapter.html#getItem%28int%29">getItem检索光标指向该用于创建使用listitem数据库的结果。现在,你可以得到的ID或不显示从数据库结果用户的所有其他值,并将其连接到一个意图,开始下一个活动。

If you are using a sqlite database to store your values you should use a CursorAdapter to manager your list items. The CursorAdapter stores the result that you got from the Database and manages how the data is displayed to the user. If you then register an onItemClickListener on your ListView the listener will receive the adapter and the position of the data item whose cell was clicked in the adapter. Now you can use getItem to retrieve a cursor pointing to the database result that was used to create the listitem. Now you can get the id or all other values that are not shown to the user from the database result and attach it to an intent to start the next activity.

这篇关于分配隐藏值到ListView项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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