Setting tags to each item in a ListView in Android

2019-02-17 17:43发布

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.

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);

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.

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?

4条回答
成全新的幸福
2楼-- · 2019-02-17 18:20

Create a ViewBinder and set the tags as the ListView is being populated with whatever you need. You can check all properties of the view to determine what tag goes where, so this should be what you're looking for.

myAdapter.setViewBinder(new MyViewBinder());

public class MyViewBinder implements ViewBinder {
    @Override
    public boolean setViewValue(View view, Object data, String text){
        //Since it iterates through all the views of the item, change accordingly 
        if(view instanceof TextView){ 
            ((TextView)view).setTag("whatever you want");
        }
    }
}

I just used this exact same answer on another question (albeit slightly different) yesterday.

查看更多
ら.Afraid
3楼-- · 2019-02-17 18:23

about getView , it works by using a method of recycling views. i will try to explain it in a simple way.

suppose you have tons of items that can be viewed . you don't want to really create tons of views too , since that would take a lot of memory . google thought of it and provide you the means to update only the views that need to be shown at any specific time.

so , if there is an empty space on the listview , it will be filled with a new view . if the user scrolls , the view that becomes hidden is recycled and given back to you on the getView , to be updated with the data of the one that is shown instead .

for example , if you scroll down , the upper view becomes hidden for the end user , but in fact it becomes the exact same view that is on the bottom .

in order to understand how to make the listview have the best performance and see in practice how and why it works as i've talked about , watch this video: http://www.youtube.com/watch?v=wDBM6wVEO70

as for tags , i think you want to do something else , since the data itself (usually some sort of collection, like an arrayList) already knows where to update , because you get the position via the getView . if you want a specific view to update , you might be able to do so by using a hashmap that keeps upadting , which its key is the position in the collection , and the value is the associated view . on each time you go to getView , you need to remove the entry that belong to the view (if exists) and assign the new position with the view that you got/created .

查看更多
叼着烟拽天下
4楼-- · 2019-02-17 18:30

I would rather go with the solution discussed in this thread. It is always the easiest to have all related data in same place and in this case you just create a class to hold all the information you will need for every item.

查看更多
我命由我不由天
5楼-- · 2019-02-17 18:37

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.

查看更多
登录 后发表回答