This article describes the ListView to add an event to the button above each item as follows:
1. First look at the effect of the picture:
Here for testing only, I write the data dead, as needed can be modified on their own, in addition to achieve ListView above each item button on the event there are two ways:
1. Use final variable to extend the lifecycle scope of a local variable main code (this article is appended with all the code):
Note that the int position variable in the original GetView method is not final and is now final @Override public view GetView (final int position, View conver
Tview, ViewGroup parent) {Viewholder holder = null;
if (Convertview = = null) {holder=new viewholder ();
It can be understood that the view is returned to ListView Convertview = Minflater.inflate (r.layout.vlist, NULL) after acquiring the view from Vlist;
Holder.title = (TextView) Convertview.findviewbyid (r.id.title);
Holder.info = (TextView) Convertview.findviewbyid (r.id.info);
HOLDER.VIEWBTN = (Button) Convertview.findviewbyid (R.ID.VIEW_BTN);
Convertview.settag (holder);
}else {holder = (Viewholder) convertview.gettag ();
} holder.title.setText ((String) mdata.get (position). Get ("title");
Holder.info.setText ((String) mdata.get (position). Get ("info");
Holder.viewBtn.setTag (position); Add a Click event to the button after the button is added ListView will lose focus need to remove the focus of the button directly HOLDER.VIewbtn.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {
Showinfo (position);
}
});
Holder.viewBtn.setOnClickListener (MyListener (position));
return convertview;
}///Extract Convenience Point public final class Viewholder {public TextView title;
public TextView info;
Public Button viewbtn;
public void Showinfo (int position) {ImageView img=new imageview (listviewactivity.this);
Img.setimageresource (r.drawable.b); New Alertdialog.builder (this). Setview (IMG). Settitle ("Details" +position). Setmessage ("Vegetable name:" +title[position]+ "Price:" +in Fo[position]). Setpositivebutton (OK), new Dialoginterface.onclicklistener () {@Override public void on
Click (dialoginterface dialog, int which) {}}). Show ();
}
2. Use the class to record the position of each button so that each button has its own listener main code:
The second method, the master generally use this method, the specific reason, I am not clear, to be studied public View getview (int position, Vie
W Convertview, ViewGroup parent) {Viewholder holder = null;
MyListener Mylistener=null;
if (Convertview = = null) {holder=new viewholder ();
It can be understood that the view is returned to ListView mylistener=new MyListener (position) after acquiring the view from Vlist;
Convertview = minflater.inflate (r.layout.vlist, NULL);
Holder.title = (TextView) Convertview.findviewbyid (r.id.title);
Holder.info = (TextView) Convertview.findviewbyid (r.id.info);
HOLDER.VIEWBTN = (Button) Convertview.findviewbyid (R.ID.VIEW_BTN);
Convertview.settag (holder);
}else {holder = (Viewholder) convertview.gettag ();
} holder.title.setText ((String) mdata.get (position). Get ("title");
Holder.info.setText ((String) mdata.get (position). Get ("info"); Holder.vieWbtn.settag (position);
Add a Click event to the button after the button is added ListView will lose focus to remove the focus of the button directly Holder.viewBtn.setOnClickListener (MyListener);
Holder.viewBtn.setOnClickListener (MyListener (position));
return convertview;
} Private class MyListener implements onclicklistener{int mposition;
Public MyListener (int inposition) {mposition= inposition; @Override public void OnClick (View v) {//TODO auto-generated method stub Toast.maketex
T (Listviewactivity.this, title[mposition], Toast.length_short). Show ();
}///Extract Convenience Point public final class Viewholder {public TextView title;
public TextView info;
Public Button viewbtn;
}
3. All code
1.listviewactivity.java All code:
Package Ms.
ListView;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;
Import android.app.Activity;
Import Android.app.AlertDialog;
Import Android.content.Context;
Import Android.content.DialogInterface;
Import Android.os.Bundle;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.view.ViewGroup;
Import Android.widget.AdapterView;
Import Android.widget.AdapterView.OnItemSelectedListener;
Import Android.widget.BaseAdapter;
Import Android.widget.Button;
Import Android.widget.ImageView;
Import Android.widget.ListView;
Import Android.widget.TextView;
Import Android.widget.Toast; The public class Listviewactivity extends activity {/** called the ' when the ' is the ' is ' the ' activity ' is a-created private list<m
Ap<string, object>> Mdata;
private int flag; public static String title[]=new string[]{"dish name 0", "Dish Name 1", "Dish Name 2", "Dish name 3", "Dish name 4", "Dish name 5", "Dish name 6", "Dish name 7", "Dish name 8 "," Dish name 9 "};
public static String info[]=new string[]{"¥:28", "¥:28", "¥:28", "¥:28", "¥:28", "¥:28", "¥:28", "¥:28", "¥:28", "¥:28",};
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Mdata = GetData ();
ListView ListView = (ListView) Findviewbyid (R.id.listview);
Myadapter adapter = new Myadapter (this);
Listview.setadapter (adapter); //Get dynamic array data can be from other places (JSON, etc.) private list<map<string, object>> GetData () {list<map<st
Ring, object>> list = new arraylist<map<string, object>> ();
for (int i=0;i<title.length;i++) {map<string, object> Map = new hashmap<string, object> ();
Map.put ("title", Title[i]);
Map.put ("info", Info[i]);
List.add (map);
} return list;
public class Myadapter extends Baseadapter {private Layoutinflater minflater; Public myadaptER (context context) {This.minflater = Layoutinflater.from (context);
@Override public int GetCount () {//TODO auto-generated a stub return mdata.size (); @Override public Object getitem (int position) {//TODO auto-generated method stub return Nu
ll
@Override public long getitemid (int position) {//TODO auto-generated method stub return 0; //****************************************final method//Note that the INT position variable in the original GetView method is not final and is now final @Over
Ride public View getview (final int position, View Convertview, ViewGroup parent) {Viewholder holder = null;
if (Convertview = = null) {holder=new viewholder ();
It can be understood that the view is returned to ListView Convertview = Minflater.inflate (r.layout.vlist, NULL) after acquiring the view from Vlist;
Holder.title = (TextView) Convertview.findviewbyid (r.id.title); Holder.info = (TextView) Convertview.findviewbyid (r.id.info);
HOLDER.VIEWBTN = (Button) Convertview.findviewbyid (R.ID.VIEW_BTN);
Convertview.settag (holder);
}else {holder = (Viewholder) convertview.gettag ();
} holder.title.setText ((String) mdata.get (position). Get ("title");
Holder.info.setText ((String) mdata.get (position). Get ("info");
Holder.viewBtn.setTag (position); Add Click event to button ListView will lose focus need to remove the focus of the button directly Holder.viewBtn.setOnClickListener (new View.onclicklisten
ER () {@Override public void OnClick (View v) {showinfo (position);
}
});
Holder.viewBtn.setOnClickListener (MyListener (position));
return convertview; }//**************************************** The second method, the master generally use this method, specific reasons, I am not clear, to be studied/public View getview (int p Osition, View Convertview, ViewGroup parent) {//Viewholder holder = null;
MyListener Mylistener=null;
if (Convertview = null) {////Holder=new viewholder ();
//Can be understood to return view to ListView//Mylistener=new MyListener (position) after acquiring view from Vlist;
Convertview = minflater.inflate (r.layout.vlist, NULL);
Holder.title = (TextView) Convertview.findviewbyid (r.id.title);
Holder.info = (TextView) Convertview.findviewbyid (r.id.info);
HOLDER.VIEWBTN = (Button) Convertview.findviewbyid (R.ID.VIEW_BTN);
Convertview.settag (holder);
}else {//Holder = (viewholder) convertview.gettag ();
////Holder.title.setText ((String) mdata.get (position). Get ("title");
Holder.info.setText ((String) mdata.get (position). Get ("info");
Holder.viewBtn.setTag (position); Add a Click event to the button after the button is added ListView will lose focus to remove the focus of the button directly//Holder.viewBtn.setOnClickListener (myListEner);
//holder.viewbtn.setonclicklistener (MyListener (position));
return convertview;
}//}////Private class MyListener implements onclicklistener{/int mposition;
Public MyListener (int inposition) {//mposition= inposition; }//@Override//public void OnClick (View v) {////TODO auto-generated a stub//to
Ast.maketext (Listviewactivity.this, title[mposition], Toast.length_short). Show ();
Blic TextView title;
public TextView info;
Public Button viewbtn;
public void Showinfo (int position) {ImageView img=new imageview (listviewactivity.this);
Img.setimageresource (r.drawable.b); New Alertdialog.builder (this). Setview (IMG). Settitle ("Details" +position). Setmessage ("Vegetable name:" +title[position]+ "Price:" +in Fo[position]). sEtpositivebutton (OK), new Dialoginterface.onclicklistener () {@Override public void OnClick (dialoginterface
dialog, int which) {}}). Show ();
}
}
2.main.xml
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:layout_width=" fill_parent "
android:layout_height=" fill_parent "
android:o" rientation= "vertical" >
<listview
android:id= "@+id/listview" android:layout_width= "Fill_"
Parent "
android:layout_height=" fill_parent "
android:divider=" @drawable/list_line "
android: dividerheight= "1dip"/>
</LinearLayout>
3.vlist.xml
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:layout_width=" fill_parent "
android:layout_height=" fill_parent "
android:o" rientation= "vertical" >
<listview
android:id= "@+id/listview" android:layout_width= "Fill_"
Parent "
android:layout_height=" fill_parent "
android:divider=" @drawable/list_line "
android: dividerheight= "1dip"/>
4.btn_detail_selecter.xml
<?xml version= "1.0" encoding= "Utf-8"?> <selector xmlns:android=
"http://schemas.android.com/apk/res/" Android >
<item android:drawable= "@drawable/btn_detail_normal" android:state_enabled= "true" Android: State_focused= "false" android:state_pressed= "false"/>
<item android:drawable= "@drawable/btn_detail_ Pressed "android:state_enabled=" true "android:state_pressed=" true "/>
<item android:drawable=" @drawable/ Btn_detail_pressed "android:state_enabled=" true "android:state_focused=" true "/>
</selector>
5.item.xml
<?xml version= "1.0" encoding= "UTF-8"?> <selector xmlns:android=
"http://schemas.android.com/apk/res/" Android >
<item android:drawable= "@drawable/item_higlight" android:state_focused= "true" Android:state_ Pressed= "false"/>
<item android:drawable= "@drawable/item_higlight" android:state_focused= "false" Android:state_pressed= "true"/>
<item android:drawable= "@drawable/item_higlight" android:state_selected = "true"/>
<item android:drawable= "@drawable/item_higlight" android:state_focused= "true"/>
< Item android:drawable= "@drawable/item_higlight"/>
6.colors.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<resources>
<color name= "Region" > #8000ff00 </ color>
<color name= "ListTitle" > #ff23323b </color>
<color name= "text" > #ff848f9b </ color>
<color name= "Write" > #ffffffff </color>
</resources>
7.values.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<string name= "Hello" >hello world, listviewactivity!</string>
<string name= "App_name" >ListView</string>
8.drawables.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<resources>
<item type= "drawable" name= "BG" ># 80000000</item>
<item type= "drawable" name= "Transparent" > #00000000 </item>
<item type = "drawable" name= "LightBlue" > #ffcfe1ed </item>
<item type= "drawable" name= "Readmenu_btn_bg_f" ># 30ffffff</item>
<item type= "drawable" name= "readmenu_btn_bg_p" > #50ffffff </item>
< Item Type= "drawable" name= "Blackmask" > #30000000 </item>
SOURCE Download: Source Download Address
Original link: http://blog.csdn.net/qq435757399/article/details/8256453
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.