We all know that the ListView is used to display a list of data, each list has a list of items, if we click to select a list, want to get the details of the list or open a new window to pass the list information in the past what to do? In this section we demonstrate the ListView click event, which we will have a deeper understanding of the ListView, first look at
Here's a look at the demo code
Main layout file
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <listview android:id= "@+id/listview1" android:layout_width= "Match_ Parent " android:layout_height=" wrap_content "> </ListView> </LinearLayout>
List item Layout file
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" 100DP "android:orientation=" vertical "and roid:gravity= "Top" > <linearlayout android:layout_width= "match_parent" android:layout_height= "Wrap_ Content "android:orientation=" horizontal "> <linearlayout android:layout_width=" match_parent " android:layout_height= "wrap_content" android:orientation= "vertical" > <textview Android : id= "@+id/tvcode" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:la yout_margintop= "5DP" android:text= "code:"/> <textview android:id= "@+id/tvname" android:l Ayout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_margintop= "5DP" Androi d:text= "Name:"/> <texTView android:id= "@+id/tvprice" android:layout_width= "wrap_content" android:layout_height= "Wrap_conten T "android:layout_margintop=" 5DP "android:text=" Price: "/> <textview android:id = "@+id/tvmodel" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layo ut_margintop= "5DP" android:text= "Size:"/> </LinearLayout> </LinearLayout> < LinearLayout android:layout_width= "fill_parent" android:layout_height= "2DP" android:background= "#F0F0F0" > </ Linearlayout></linearlayout>
Background code
public class Listviewclickactivity extends Activity {private ListView LV; Simpleadapter adp;//defines the adapter private list<map<string,object>> maplist;//defines the data source protected void OnCreate ( Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview ( R.layout.listviewclicklay); lv= (ListView) Findviewbyid (r.id.listview1); Maplist=new arraylist<map<string, Object>> (); for (int i=0;i<10;i++) {map<string,object> map=new hashmap<string,object> (); Map.put ("Code", "Code: +i"); Map.put ("name", "title: Ipad" +i); Map.put ("Price", "Prices:" +i); Map.put ("Model", "unit:" +i); Maplist.add (map); } adp=new Simpleadapter (Listviewclickactivity.this, Maplist,r.layout.listdetail, new string[]{"code", "name", "Price "," Model "}, new Int[]{r.id.tvcode,r.id.tvname,r.id.tvprice,r.id.tvmodel}); Lv.setadapter (ADP); Lv.setonitemclicklistener (New Onitemclicklistener () { @Override public void Onitemclick (adapterview<?> arg0,view arg1, int arg2, Lon G Arg3) {TextView tname= (TextView) Arg1.findviewbyid (r.id.tvname);//Name Tex TView tmodel= (TextView) Arg1.findviewbyid (R.id.tvmodel);//Specification TextView tprice= (TextView) Arg1.findviewbyid ( R.id.tvprice);//Unit Price TextView tcode= (TextView) Arg1.findviewbyid (R.id.tvcode);//Code Toast.mak EText (Getapplicationcontext (), "Current product Name:" +tname.gettext () + ", Code:" +tcode.gettext (), (). Show (); } }); }}
. NET programmer play to Android Development---() ListView Click event