ListView is a more commonly used control, but has always felt that the creation of a ListView step is a bit cumbersome, so summarize here for easy access.
The program effect is to implement a listview,listview inside there is a title, content and picture, and add click and long press Response.
First define a ListView in XML
<?xml version= "1.0" encoding= "Utf-8"?><linearlayout android:id= "@+id/linearlayout01" android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" xmlns: android= "http://schemas.android.com/apk/res/android" ><listview android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:id= "@+id/listview01" />< /linearlayout>
Main.xml
Defines the layout of each entry in the ListView, implemented with Relativelayout:
<?xml version= "1.0" encoding= "Utf-8"?><relativelayout Android:id= "@+id/relativelayout01"Android:layout_width= "Fill_parent"xmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_height= "Wrap_content"Android:paddingbottom= "4dip"Android:paddingleft= "12dip"Android:paddingright= "12dip" ><ImageView android:paddingtop= "12dip"Android:layout_alignparentright= "true"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/itemimage"/> <TextView Android:text= "TextView01"Android:layout_height= "Wrap_content"android:textsize= "20dip"Android:layout_width= "Fill_parent"Android:id= "@+id/itemtitle"/><TextView Android:text= "TextView02"Android:layout_height= "Wrap_content"Android:layout_width= "Fill_parent"Android:layout_below= "@+id/itemtitle"Android:id= "@+id/itemtext"/></relativelayout>
List_items.xml
Finally, call and join listener in activity, see note:
Packagecom.ray.test;Importjava.util.ArrayList;ImportJava.util.HashMap;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.ContextMenu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.ContextMenu.ContextMenuInfo;ImportAndroid.view.View.OnCreateContextMenuListener;ImportAndroid.widget.AdapterView;ImportAndroid.widget.ListView;ImportAndroid.widget.SimpleAdapter;ImportAndroid.widget.AdapterView.OnItemClickListener; Public classTestlistviewextendsActivity {@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.main); //bind the ListView inside the layoutListView list =(ListView) Findviewbyid (R.ID.LISTVIEW01); //generating dynamic arrays, adding dataarraylistNewArraylist(); for(inti=0;i<10;i++) {HashMap<string, object> map =NewHashmap<string, object>(); Map.put ("Itemimage", r.drawable.checked);//ID of the image resourceMap.put ("Itemtitle", "level" +i); Map.put ("Itemtext", "finished in 1 Min Secs, moves!"); Listitem.add (map); } //build the adapter's item and the element corresponding to the dynamic arraySimpleadapter Listitemadapter =NewSimpleadapter ( This, ListItem,//Data SourceR.layout.list_items,//XML implementation of ListItem//the children of the dynamic array corresponding to the Imageitem NewString[] {"Itemimage", "Itemtitle", "Itemtext"}, //imageitem XML file inside of a imageview, two TextView ID New int[] {r.id.itemimage,r.id.itemtitle,r.id.itemtext}); //Add and showList.setadapter (Listitemadapter); //Add ClickList.setonitemclicklistener (NewOnitemclicklistener () {@Override Public voidOnitemclick (adapterview<?> arg0, View arg1,intArg2,LongArg3) {Settitle ("Click on" +arg2+ "items"); } }); //Add long Press to clickList.setoncreatecontextmenulistener (NewOncreatecontextmenulistener () {@Override Public voidOncreatecontextmenu (ContextMenu menu, View v,contextmenuinfo menuinfo) {menu.setheadertitle ("Long Press Menu-contextmenu"); Menu.add (0, 0, 0, "Popup Long press Menu 0"); Menu.add (0, 1, 0, "Popup Long Press Menu 1"); } }); } //long-Press Menu response function@Override Public Booleanoncontextitemselected (MenuItem Item) {Settitle ("Click on the" +item.getitemid () + "items" in the long press Menu); return Super. oncontextitemselected (item); }}
Testlistview. Java
Common usage of Android ListView (i)