The display of a list requires three elements:
1. Listveiw The view used to display the list.
2. The adapter is used to map data to mediations on the ListView.
3. The data is specific to the string, picture, or base component that will be mapped.
Based on the list of adapter types, the list is divided into three types, arrayadapter,simpleadapter and Simplecursoradapter
Ui:
1<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"2Xmlns:tools= "Http://schemas.android.com/tools"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent"5android:paddingbottom= "@dimen/activity_vertical_margin"6android:paddingleft= "@dimen/activity_horizontal_margin"7android:paddingright= "@dimen/activity_horizontal_margin"8android:paddingtop= "@dimen/activity_vertical_margin"9Tools:context= ". Mainactivity ">Ten One<ListView AAndroid:id= "@+id/listview1" -Android:layout_width= "Match_parent" -android:layout_height= "Wrap_content" theAndroid:layout_alignparentleft= "true" -android:layout_marginleft= "26DP" > - -</ListView> + -</RelativeLayout>
ListView
Activity:
1 PackageCom.example.android_listview;2 3 Importjava.util.ArrayList;4 Importjava.util.List;5 6 Importandroid.app.Activity;7 ImportAndroid.os.Bundle;8 ImportAndroid.view.Menu;9 ImportAndroid.view.View;Ten ImportAndroid.widget.AdapterView; One ImportAndroid.widget.AdapterView.OnItemClickListener; A ImportAndroid.widget.ArrayAdapter; - ImportAndroid.widget.ListView; - ImportAndroid.widget.Toast; the /** - * @authorXiaowu - * Use of Note:listview - */ + Public classMainactivityextendsActivity { - PrivateListView ListView; + PrivateArrayadapter<string>Arrayadapter; A @Override at protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); -ListView =(ListView) Findviewbyid (r.id.listview1); - /* - * Set the check mode of the ListView in * Multiple choice: choice_mode_multiple - * None: Choice_mode_none to * Radio: Choice_mode_single + * When setting the check mode, be careful to modify the display style of the Arrayadapter array adapter. - */ the Listview.setchoicemode (listview.choice_mode_single); *Arrayadapter =NewArrayadapter<string> (mainactivity. This, Android. R.layout.simple_list_item_single_choice, GetData ()); $ //register the adapter for the ListView, if the adapter needs to be with new data, the UI presentation information needs to be updated using the Notifydatasetchanged () method (once the adapter has data, notify the UI update directly)Panax Notoginseng Listview.setadapter (arrayadapter); - //Register Click to select Event theListview.setonitemclicklistener (NewOnitemclicklistener () { + @Override A Public voidOnitemclick (adapterview<?>Parent, view view, the intPositionLongID) { + //TODO auto-generated Method Stub -Toast.maketext (mainactivity. This, "select" +arrayadapter.getitem (position), 0). Show (); $ } $ }); - } - PublicList<string>GetData () { thelist<string> list =NewArraylist<string>(); - for(inti = 0; i<30; i++){WuyiList.add ("HW" +i); the } - returnlist; Wu } - @Override About Public BooleanOncreateoptionsmenu (Menu menu) { $ //inflate the menu; This adds items to the action bar if it is present. - getmenuinflater (). Inflate (R.menu.main, menu); - return true; - } A +}
Android Development-listview List Display control implementation