Then the previous article "Android uses layout inflater to create a View object", this article uses the common data adapter Arrayadapter
After you create a new project, create a new List_item.xml file under the Layout folder, and then edit the layout with the following code:
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal" > <ImageViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:src= "@drawable/ic_launcher" /> <TextViewAndroid:id= "@+id/tv"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" /></LinearLayout>
Then add the code in the mainactivity:
PackageCom.wuyudong.arrayadapter;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.view.Menu;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.ListView; Public classMainactivityextendsActivity {Private StaticString[] names = {"Function 1", "Function 2", "Function 3", "Function 4", "function 5" }; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); ListView LV=(ListView) Findviewbyid (r.id.lv); Lv.setadapter ( New arrayadapter<string> ( This , R.layout.list_item, r.id.tv, names)); }}
Run it:
Arrayadapter has many constructors, and the meaning of the parameters can be found in the source code
/*** Constructor * *@paramcontext the current context. * @paramResource The resource ID for a layout file containing a TextView to use when * instantiating view S.*/ PublicArrayadapter (Context context,intResource) {init (context, resource,0,NewArraylist<t>()); } /*** Constructor * *@paramcontext the current context. * @paramResource The resource ID for a layout file containing a layout to use when * instantiating views. * @paramTextviewresourceid The ID of the TextView within the layout resource to be populated*/ PublicArrayadapter (Context context,intResourceintTextviewresourceid) {init (context, resource, Textviewresourceid,NewArraylist<t>()); } /*** Constructor * *@paramcontext the current context. * @paramResource The resource ID for a layout file containing a TextView to use when * instantiating view S. *@paramobjects the objects to represent in the ListView. */ PublicArrayadapter (Context context,intresource, t[] objects) {init (context, resource,0, Arrays.aslist (objects)); } /*** Constructor * *@paramcontext the current context. * @paramResource The resource ID for a layout file containing a layout to use when * instantiating views. * @paramTextviewresourceid The ID of the TextView within the layout resource to be populated *@paramobjects the objects to represent in the ListView. */ PublicArrayadapter (Context context,intResourceintTextviewresourceid, t[] objects) {init (context, resource, Textviewresourceid, Arrays.aslist (objects)); } /*** Constructor * *@paramcontext the current context. * @paramResource The resource ID for a layout file containing a TextView to use when * instantiating view S. *@paramobjects the objects to represent in the ListView. */ PublicArrayadapter (Context context,intResource, list<t>objects) {init (context, resource,0, objects); } /*** Constructor * *@paramcontext the current context. * @paramResource The resource ID for a layout file containing a layout to use when * instantiating views. * @paramTextviewresourceid The ID of the TextView within the layout resource to be populated *@paramobjects the objects to represent in the ListView. */ PublicArrayadapter (Context context,intResourceintTextviewresourceid, list<t>objects) {init (context, resource, Textviewresourceid, objects); }
Android Common Data Adapter Arrayadapter