Reprinted: http://www.apkbus.com/android-13264-1-1.html
Write a custom adapter to bind the listview. When rewriting getview, you will find that the online reference code is useful to the settag () and gettag () functions.
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.
The sample code is as follows:
Public class mainactivity 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 );
Mylistener listener = new mylistener ();
Button1.settag (1 );
Button1.setonclicklistener (listener );
Button2.settag (2 );
Button2.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;
}
}
}
}