Android Common Data Adapter Arrayadapter

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.