1

I want to know if it is good practice to save some metadata to the View's tag. I have this code:

TableRow file = new TableRow(getActivity());
file.setTag(<Object_with_data>);

And in the OnClickListener:

file.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Object_with_data instanse1 = 
                    (Object_with_data) view.getTag();
        }
});
Ola Ström
  • 4,136
  • 5
  • 22
  • 41
Mike Minaev
  • 1,912
  • 4
  • 23
  • 33

1 Answers1

1

Yes I think it is a good practice. According to the official doc :

http://developer.android.com/reference/android/view/View.html#setTag(java.lang.Object)

A tag can be used to mark a view in its hierarchy and does not have to be unique within the hierarchy. Tags can also be used to store data within a view without resorting to another data structure.

and another question on StackOverflow :

What is the main purpose of setTag() getTag() methods of View?

It's basically a way for views to have memories.

The View's tag seems to be created to allows you to actually store metadata.

Community
  • 1
  • 1
mithrop
  • 3,283
  • 2
  • 21
  • 40