为 Android 中的 ListView 中的每个项目设置标签? [英] Setting tags to each item in a ListView in Android?

查看:21
本文介绍了为 Android 中的 ListView 中的每个项目设置标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ListView,我希望每个项目都附有一个 ID 号(与位置号不同).我希望这可以通过在创建这些视图时使用 setTag() 为 ListView 中的每个视图项设置一个标签来完成.

I have a ListView where I want each item to have an ID number attached to it (not the same as the position number). I was hoping this could be done by setting a tag to each View item in the ListView using setTag() when these Views are being created.

现在我正在像这样创建 ListView:

Right now I'm creating the ListView like this:

    final ListView listview = (ListView) findViewById(R.id.listView1);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, names);
    listview.setAdapter(adapter);

上面 ArrayAdapter 参数中的 names 变量是一个 ArrayList,这个列表中的每个字符串值也有一个唯一的 ID,我想以某种方式链接到这个字符串.

The names variable in the ArrayAdapter parameters above is an ArrayList, and each string value in this list also has a unique ID that I want to link to this string somehow.

有什么方法可以访问和修改带有标签的每个视图?一个想法是创建我自己的 ArrayAdapter 扩展类并覆盖 getView() 方法,但我并不真正了解它是如何工作的以及我将如何去做.

Is there any way I can get access to and modify each of the Views with a tag? One idea was to create my own extended class of ArrayAdapter and override the getView() method, but I don't really understand how it works and how I would go about doing this.

或者有没有更好的方法来像这样将 ID 与每个字符串链接起来,而不是像我试图做的那样添加标签?

Or is there a better way to link IDs with each string like this than adding tags like I'm trying to do?

推荐答案

感谢您的回答.thisMayhem 的答案最终可能会更容易一些,但是在我寻求了解更多信息的过程中,我最终根据 本教程.我将名称和 ID 传递给适配器,并将名称设置为 TextViews 的文本,将 ID 设置为标签.

Thanks for the answers. thisMayhem's answer would probably have been easier in the end, but on my quest to learn more I ended up making my own adapter according to this tutorial. I pass down the names and the IDs into the adapter and set the names as the text of the TextViews and the IDs as the tags.

这篇关于为 Android 中的 ListView 中的每个项目设置标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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