A ListView is the content of a set of lists that can be scrolled vertically, each of which can be a separate component or a combination of multiple components.
To implement a ListView control's step-by:
1, prepare the ListView to display the data, usually a one-dimensional or two-dimensional dynamic array.
2, build the adapter, because each entry in the ListView can be very simple, it can be very complex, depending on the need to choose Arrayadapter, Simpleadapter, or Baseadapter.
3. Use Setadapter to set the adapter for a ListView control.
4. Add listeners to the ListView.
Here's a look at the specific case:
Create the main interface, add a ListView control
File name is: Activity_main.xml
It is necessary to note that the ListView ID is @android:id/list, otherwise nullpointexception
<linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"xmlns:tools="Http://schemas.android.com/tools"Android:layout_width="Match_parent"android:layout_height="Match_parent"Android:paddingbottom="@dimen/activity_vertical_margin"android:paddingleft="@dimen/activity_horizontal_margin"android:paddingright="@dimen/activity_horizontal_margin"android:paddingtop="@dimen/activity_vertical_margin"tools:context=". Mainactivity "> <listview android:id="@android: Id/list"Android:layout_width="Fill_parent"android:layout_height="Wrap_content"></listview></Linearlayout>
Create an Entry interface
File name is: Item.xml
<LinearLayout xmlns:android="Http://schemas.android.com/apk/res/android" Xmlns:tools="Http://schemas.android.com/tools" Android:layout_width="Match_parent" Android:layout_height="Match_parent" Android:paddingbottom="@dimen/activity_vertical_margin" Android:paddingleft="@dimen/activity_horizontal_margin" Android:paddingright="@dimen/activity_horizontal_margin" Android:paddingtop="@dimen/activity_vertical_margin" android:orientation="Horizontal"> <ImageViewandroid:contentdescription= "@null"android:id="@+id/ IMG "android:layout_width="wrap_content "android:layout_height=" wrap_content "/> <linearlayoutandroid:orientation="vertical"android:layout_width ="Wrap_content"android:layout_height="Wrap_content" > <textview android:id= "@+id/title" android:layout_width =
"wrap_content"
android:layout_height = "wrap_content" android:textcolor =" #CCCC99 "/> <TextViewandroid:id= "@+id/info"android:layout_width="Wrap_ Content "android:layout_height=" Wrap_content "android:textcolor=" #CCCC99 " /> </linearlayout></linearlayout>
Specific logic code
PackageCom.yangzi.listview;ImportJava.util.ArrayList;ImportJava.util.HashMap;ImportJava.util.List;ImportJava.util.Map;Importandroid.app.ListActivity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.AdapterView;ImportAndroid.widget.AdapterView.OnItemClickListener;ImportAndroid.widget.ListView;ImportAndroid.widget.SimpleAdapter; Public class mainactivity extends listactivity{ PrivateListView list;@Override protected void onCreate(Bundle savedinstancestate) {//TODO auto-generated method stub Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main);//Find the ListView controlList = (ListView) Findviewbyid (Android. R.id.list);//Build adapter for item and dynamic array corresponding elementsConstructor prototypes: Public Simpleadapter(context context, list<? extends Map<string,?>> data,intResource, string[] from,int[] to) parameter description:Context( This) Current Activity ObjectData(GetData()) To display the data, List collection typeResourceLayout file for (r.layout.item) entries from(new array) The key name of the map in the data parameter to match the key name of the map in the generated to(new array) entry in layout file for display of controls */Simpleadapter adapter =NewSimpleadapter ( This, GetData (), R.layout.item,Newstring[]{"img","title","Info"},New int[]{r.id.img,r.id.title,r.id.info}];//Set adapterList.setadapter (adapter);//Set Listener eventsList.setonitemclicklistener (NewOnitemclicklistener () {@Override Public void Onitemclick(adapterview<?> arg0, View arg1,intArg2,LongARG3) {map<string,object> Clkmap = (map<string,object>) arg0.getitematposition (ARG2); Settitle (Clkmap.get ("title"). ToString () +"The URL is:"+clkmap.get ("Info"). ToString ()); } }); }PrivateList<map<string,object>>GetData(){//This can be viewed as a corresponding ListView control in each itemArraylist<map<string,object>> ListItem =NewArraylist<map<string,object>> ();//This can be seen as the specific data in each item in the ListView controlmap<string,object> map =NewHashmap<string, object> (); Map.put ("img", r.drawable.xx);//place imgMap.put ("title","Baidu");//Place titleMap.put ("Info","Http://www.baidu.com");//Place InfoListitem.add (map);//A record is set up, add it inMap =NewHashmap<string, object> (); Map.put ("img", r.drawable.xx); Map.put ("title","Sina"); Map.put ("Info","http://www.sina.com.cn"); Listitem.add (map);///below to see the effect of the ListView control more clearly, use a loop to add multiple records for(inti =0; I <Ten; i++) {map =NewHashmap<string, object> (); Map.put ("img", r.drawable.xx); Map.put ("title","My Site"+i); Map.put ("Info","Http://[email protected]"+i); Listitem.add (map); }returnListItem }}
ListView use Case in Android (Simpleadapter implementation)