Android setTag () and getTag (), and set multiple setTag ()
First, we need to know what the setTag method is.
Tags
Unlike IDs, tags are not used to identify views. tags are essentially an extra piece of information that can be associated with a view. they are most often used as a convenience to store data related to views in the views themselves rather than by putting them in a separate structure.
Unlike the ID, a Tag indicates a view. In essence, a Tag is the additional information of the associated view.
They are often used to store some view data, which is very convenient and does not need to be stored in another separate structure.
Tag has a feature that binds data to a specified control (view) without displaying it.
SetTag is a useful method in the view class of android,
Unlike setId (), findViewById finds the object itself.
The setTag () obtains the object to which the object points.
In the same class, it is convenient to access data without database creation,
It also saves more memory than sharePreference.
The setTag (Object tag) method is relatively simple.
For example
TextView tvExecutor = (TextView) findViewById (R. id. t );
TvExecutor. setTag(SelectedUserMap );
This selectedUserMap
Map SelectedUserMap.
It can also be the upload list. MList object.
It can store a variety of temporary data, but it cannot be called a storage method.
In development, we can use setTag () and getTag () to access data.
Can I set multiple tags for one object? The answer is yes.
Add
In activity, it is written
TvExecutor. setTag (R. id. tag_a, "Tease ratio"); tvExecutor. setTag (R. id. tag_ B, "2 goods ");
In fact, convertView is the most used
Tags in convertView
1. Use the Tag that uses the LayoutInflater object to expand the View
Previously, in the adapter, we had some code in getView:
PublicView getView (IntPosition, View convertView, ViewGroup parent ){
ViewHolder holder =Null;
If(ConvertView =Null){
Holder =NewViewHolder ();
ConvertView = inflater. inflate (R. layout.Vlist2,Null);
Holder. img = (ImageView) convertView. findViewById (R. id.Img);
Holder. title = (TextView) convertView. findViewById (R. id.Title);
Holder.info = (TextView)
ConvertView. findViewById (R. id.Info);
// Wonderful use of setTag
ConvertView. setTag (holder );
}Else{
Holder = (ViewHolder) convertView. getTag ();
}
...... Omitted
}
Example 3 In The onClick event, use the tag
SetTag ()/getTag ()
The setTag (Onbect) in the View indicates that an extra data is added to the View. In the future, you can use getTag () to retrieve this data.
You can use multiple buttons to add a listener. Each Button has a different setTag. The listener uses getTag to identify which Button is pressed.
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. Button;
Public class Main extends Activity {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Button button1 = (Button) findViewById (R. id. Button01 );
Button button2 = (Button) findViewById (R. id. Button02 );
Button button3 = (Button) findViewById (R. id. Button03 );
Button button4 = (Button) findViewById (R. id. Button04 );
MyListener listener = new MyListener ();
Button1.setTag (1 );
Button1.setOnClickListener (listener );
Button2.setTag (2 );
Button2.setOnClickListener (listener );
Button3.setTag (3 );
Button3.setOnClickListener (listener );
Button4.setTag (4 );
Button4.setOnClickListener (listener );
}
Public class MyListener implements View. OnClickListener {
@ Override
Public void onClick (View v ){
Int tag = (Integer) v. getTag ();
Switch (tag ){
Case 1:
System. out. println ("button1 click ");
Break;
Case 2:
System. out. println ("button2 click ");
Break;
Case 3:
System. out. println ("button3 click ");
Break;
Case 4:
System. out. println ("button4 click ");
Break;
}
}
}
}