为 ListView 设置唯一 ID Android
Setting Unique Id to ListView Android
我有一个 ListView,它显示数据库中的人员信息。是否可以向列表视图中的每个项目添加一个特定的 ID,如 personid,以便我可以使用该 ID 在单击列表视图项目时传递到另一个页面。我正在为 ListView 使用 SimpleAdapter。
我看到了 link here 。但这并不能完全满足我的要求。
有没有办法直接设置 Id ,就像我们在 List of hashmaps for setting in SimpleAdapter
中所做的那样
当您使用 SimpleAdapter 时,您为它提供了一个映射数组列表。
你应该做的是将你的 personid 作为一个条目包含在你的地图中,你不必将它绑定到你行中的任何显示元素,因此它不必显示。
比如你的地图叫rowMap,id叫personId
rowMap.put("person_id", personId);
adapterList.add(rowMap);
然后当您的项目被选中时,您可以使用传递给您的侦听器的位置来检索地图。一旦你有了它,只需调用
map = adapterList.get(pos);
selectedId = map.get("person_id");
更新了更多关于 SimpleAdapter 的信息
这里有一些关于 SimpleAdapter 的更多信息,下面是关于构造函数的文档以供参考。
想法是创建一个地图 ArrayList,每个地图条目必须 至少 要显示的数据,但它可以包含 更多
比你想要显示的数据。
您提供的 from
、 to
数组使您提供的地图条目适应您提供的视图。如果您在地图条目中包含额外信息,它会被忽略并且不会显示。所以在这种情况下,假设您不想显示 personid,您可以将 personid 添加到 Map 条目,但不会将其添加到 from
和 to
数组
public SimpleAdapter (Context context, List>
data, int resource, String[] from, int[] to)
Added in API level 1 Constructor
Parameters
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.
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.
我有一个 ListView,它显示数据库中的人员信息。是否可以向列表视图中的每个项目添加一个特定的 ID,如 personid,以便我可以使用该 ID 在单击列表视图项目时传递到另一个页面。我正在为 ListView 使用 SimpleAdapter。 我看到了 link here 。但这并不能完全满足我的要求。 有没有办法直接设置 Id ,就像我们在 List of hashmaps for setting in SimpleAdapter
中所做的那样当您使用 SimpleAdapter 时,您为它提供了一个映射数组列表。 你应该做的是将你的 personid 作为一个条目包含在你的地图中,你不必将它绑定到你行中的任何显示元素,因此它不必显示。
比如你的地图叫rowMap,id叫personId
rowMap.put("person_id", personId);
adapterList.add(rowMap);
然后当您的项目被选中时,您可以使用传递给您的侦听器的位置来检索地图。一旦你有了它,只需调用
map = adapterList.get(pos);
selectedId = map.get("person_id");
更新了更多关于 SimpleAdapter 的信息
这里有一些关于 SimpleAdapter 的更多信息,下面是关于构造函数的文档以供参考。
想法是创建一个地图 ArrayList,每个地图条目必须 至少 要显示的数据,但它可以包含 更多 比你想要显示的数据。
您提供的 from
、 to
数组使您提供的地图条目适应您提供的视图。如果您在地图条目中包含额外信息,它会被忽略并且不会显示。所以在这种情况下,假设您不想显示 personid,您可以将 personid 添加到 Map 条目,但不会将其添加到 from
和 to
数组
public SimpleAdapter (Context context, List> data, int resource, String[] from, int[] to)
Added in API level 1 Constructor
Parameters
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.
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.