设置唯一的ID的ListView的Andr​​oid [英] Setting Unique Id to ListView Android

查看:120
本文介绍了设置唯一的ID的ListView的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListView其中显示了从数据库中人员的信息。这有可能像PERSONID添加特定的ID每个项目在列表视图,这样我就可以使用该ID将被传递到另一个页面上单击列表视图项。我使用为ListView一个SimpleAdapter。
我在这里看到一个链接 。但是那并不'T完全满足我的要求。
有没有办法直接设置标识,就像我们在包含HashMap的名单中SimpleAdapter设置做

I have a ListView which shows person information from database. Is that possible to add a particular ID like personid to each item in listview, so that I can use that ID to be passed to another page on clicking the list view item . I am using a SimpleAdapter for the ListView. I have seen a link here .But that doesn't fully satisfies my requirement. Is there a way to directly set the Id , like what we do in the List of hashmaps for setting in SimpleAdapter

推荐答案

当您使用SimpleAdapter,你为它提供地图的ArrayList。
你应该做的是包括你PERSONID作为你的地图中的条目,您不必将其绑定到你的行中的任何显示元件,所以它并没有显示出来。

When you use SimpleAdapter, you provide it with an ArrayList of Maps. What you should do is include your personid as an entry in your Map, you don't have to bind it to any display element in your row, so it does not have to be displayed.

例如,如果你的地图被称为rowMap,而id叫PERSONID

For example if your map is called rowMap, and the id is called personId

rowMap.put("person_id", personId);
adapterList.add(rowMap);

然后在选择你的项目,你检索使用传递到您的收听位置地图。一旦你的,只需拨打

Then when your item is selected, you retrieve the Map using the position that is passed to your listener. Once you have that, just call

map = adapterList.get(pos);
selectedId = map.get("person_id");

更新与在SimpleAdapter多一点信息

下面是关于SimpleAdapter一点更多的信息,下面是关于构造参考文档。

Updated with a little more info on the SimpleAdapter

Here is a little more info on the SimpleAdapter, below is the doc on the constructor for reference.

我们的想法是您创建地图的ArrayList,每个映射项必须具有的至少要显示的数据,但它可以包含的更多
 数据不是要显示的内容。

The idea is that you create an ArrayList of Maps, each map entry has to have at least the data you want to display, but it can contain more data than what you want to display.

您提供适应您提供给您提供的视图中的地图项中的阵列。如果包含在映射项额外的信息,它只是忽略,不显示。因此,在这种情况下,假设你不想显示PERSONID,可以添加到PERSONID地图输入,而不是从添加到阵列

The from , to arrays that you provide adapt the map entry that you provide to the view that you provide. If you include extra info in the map entry it is simply ignored and not displayed. So in this case, assuming you don't want to display the personid, you would add personid to the Map entry, but not add it to the from , and to arrays

公共SimpleAdapter(上下文的背景下,名单>
  数据,INT资源的String []从,INT []到)

public SimpleAdapter (Context context, List> data, int resource, String[] from, int[] to)

在API级别1构造

参数

环境:那里的查看与此相关的背景
  SimpleAdapter运行

context: The context where the View associated with this SimpleAdapter is running

数据:地图列表。在列表中的每个条目
  对应于在列表中的一行。该地图包含每个数据
  行,应包括从中指定的所有项

data: A List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in "from"

资源:一个视图布局资源标识符定义的意见
  这个列表项目。布局文件至少应包括那些
  在定义了一个名为意见,以

resource: Resource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in "to"

来自:列名的名单,这将是
  添加到与每个项目相关的地图。

from: A list of column names that will be added to the Map associated with each item.

要:该观点认为应
  在从参数显示列。这些都应该是TextViews。
  在此列表中的前N次给予前N的值
  在从参数列。

to: The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.

这篇关于设置唯一的ID的ListView的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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