如何为 ListView 的每一行设置行 ID?

发布于 2024-09-29 02:44:08 字数 256 浏览 1 评论 0原文

我正在开发一个 Android 应用程序。

如何为 ListView 上的每个项目设置行 ID?我不想使用 SimpleCursorAdapter。我有自己的物品,我想使用它们。

这些对象具有 ID 和 NAME。将显示名称并存储 ID,以便在 onListItemClick(ListView Parent, View v, int position, long id) 上检索它。

有什么建议吗?

谢谢。

I'm developing an Android application.

How can I setup the row id for every item on a ListView? I don't want to use SimpleCursorAdapter. I have my owns objects, and I want to use them.

These objects have and ID and a NAME. The name will be displayed and the ID will be stored to retrieve it on onListItemClick(ListView parent, View v, int position, long id).

Any advice?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小女人ら 2024-10-06 02:44:08

一种简单的解决方案是创建一个将名称映射到 id 的哈希值:

Map<String, Integer> map = new HashMap<String, Integer>();
for(MyObject obj: objectsList) {
  map.put(obj.getName(), Integer.valueOf(obj.getId()));
}



String name = getListView().getItemAtPosition((int) id).toString();
int id = map.get(name).intValue();

One simple solution would be to create a hash that maps names to ids:

Map<String, Integer> map = new HashMap<String, Integer>();
for(MyObject obj: objectsList) {
  map.put(obj.getName(), Integer.valueOf(obj.getId()));
}



String name = getListView().getItemAtPosition((int) id).toString();
int id = map.get(name).intValue();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文