tags:

views:

243

answers:

1

Because Java uses object references and not objects themselves, what prevents me from using setTag() to tag a view with an entire object instead of an object's property? Is it just the attribute lookup time when trying to resolve one of the attributes after the getTag() call or is there any other specific thing I should be concerned about?

As for my specific problem, I am using a custom listview that has an imageview and a textview. Initially I bind the listview to a custom adapter to fetch some xml data and then use certain tags inside each item's xml to populate my listview. So the "entire object" I was referring to was the parsed version of the entire XML of an item...

+1  A: 

One of the most popular uses of the setTag(Object) method is exactly keeping a reference to a class instance - if you have used custom ListView and custom Adapter, you should know about the ViewHolder pattern.

Without knowing much about your particular problem, I would say - Is this dangerous sometimes? Yes, if used irresponsibly. Does this mean you should avoid it at any cost? No, absolutely not.

Edit: Why do you want to have the parsed data for your views bound to them?

Do you really need it, or you can populate some type of a model? If you want to access the tag of a view in a context, where your view doesn't carry the same meaning/position (like the convertView does in our favorite ViewHolder example :)), I would think using tags is OK.

Otherwise, I'm sure that if you give it a little more thought, you'll find another approach better suited for your problem.

Dimitar Dimitrov
Yes. I've heard of the ViewHolder pattern. Thanks... That's a relief...About my problem, I've just updated my question above...
Legend
Its just that the item description keeps changing depending on which item is being viewed. So if I have to pass some information to an intent, I need some extra information about this particular item under consideration. Of course, I think I exaggerated by saying that I would pass the entire item but just one or two attributes is fine for me. But, I wanted to just know from a performance perspective... Thanks for the reply...
Legend