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

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

问题描述

我有一个ListView,我想每个项目有连接到它的ID号(不一样的位置编号)。我希望这可以通过使用标签设置为每个视图项在ListView做 setTag()正在创建这些视图时。

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参数

名称变量是一个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.

还是有更好的方式来与每个字符串像这样不是添加标签就像我试图做链接标识?

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

推荐答案

感谢您的答案。到底有多我最终根据的本教程。我传下的名称和的ID插入适配器,并设置名称为TextViews的文字和标识的标签。

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天全站免登陆