The ListView is the most commonly used view list in Android and needs to be done by using a data adapter to display the obtained data in the ListView. The most commonly used adapters are arrayadapter and simpleadapter. It can be said that the adapter is a bridge connecting the data source and the View interface
Arrayadapter is commonly used in array data, and the binding data format is relatively single.
Simpleadapter is commonly used in a specific generic collection, and the data format of the binding is complex.
To display the interface on the view with an adapter:
1. Create a new adapter
2. Loading the data source with an adapter
3. View Load Adapter
I.Arrayadapter
Public classMainactivityextendsActivity {PrivateListView ListView; PrivateArrayadapter<string>Arradapter; PrivateSimpleadapter Simadapter; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); ListView=(ListView) Findviewbyid (R.id.listview); String[] Arrdata=NewString[20]; for(inti = 0; I < 20; i++) {Arrdata[i]= "Item" +i; } //1. Create a new adapter /** Context: current instance this * Textviewresourceid:listview child layout ID, which is the currently loaded layout file * Objects: Data source /c10>*/ //2. Add a data sourceArradapter =NewArrayadapter<string> ( This, Android. R.layout.simple_list_item_1, Arrdata); //3. View Load AdapterListview.setadapter (Arradapter); }}
Show after run
Two. Simpleadapter
Public classMainactivityextendsActivity {PrivateListView ListView; PrivateSimpleadapter Simadapter; PrivateList<map<string, object>>ListData; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); ListView=(ListView) Findviewbyid (R.id.listview); //1. Create a new adapter /** Context: Current instance this data: DataSource list<? extends Map<string,?>> a List collection containing MAP * resource: Current The key name in the loaded data source From:map to: The ID in the bound view corresponds to the Map in the From*/ //2. Loading the data sourceListData =NewArraylist<map<string,object>>(); Simadapter=NewSimpleadapter ( This, Getlistdata (), R.layout.item,NewString[] {"Image", "Text"},New int[] {r.id.image, r.id.text}); //3. View Load AdapterListview.setadapter (Simadapter); } PrivateList<map<string, object>>Getlistdata () { for(inti = 0; I < 20; i++) {Map<string, object> map =NewHashmap<string, object>(); Map.put ("Image", R.drawable.ic_launcher); Map.put ("Text", "Textdemo" +i); Listdata.add (map); } returnListData; }}
There are many constructor parameters in Simpleadapter, data source is a list collection that contains a map. Each of these maps corresponds to each row in the ListView, which specifies the key names contained in the map, that is, the key names in the map in Getlistdata must be "image" and "text". To is the ID of the view control that corresponds to each key name. The following views are loaded as resource:
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Horizontal" > <ImageViewAndroid:id= "@+id/image"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:src= "@drawable/ic_launcher"android:padding= "10DP" /> <TextViewAndroid:id= "@+id/text"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:textsize= "20SP"Android:textcolor= "#000000"android:gravity= "Center"Android:layout_margintop= "10DP"Android:text= "Demo"/> </LinearLayout>
That is, each row of the ListView is made up of ImageView and TextView. Where the image key value and R.id.iamge point to Imageview,text key value and R.id.text point to TextView. Picture below:
The program runs as follows:
Android ListView drop-down list