Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting tags to each item in a ListView in Android?

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?

like image 501
mattboy Avatar asked May 17 '12 22:05

mattboy


2 Answers

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.

like image 143
Juan Cortés Avatar answered Sep 24 '22 05:09

Juan Cortés


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 .

like image 30
android developer Avatar answered Sep 24 '22 05:09

android developer