Android notes: ListView and Arrayadapter

Source: Internet
Author: User

The ListView is used to display large amounts of data, and data cannot be passed directly to the ListView, which needs to be done with adapter adapter.

Arrayadapter is the most commonly used adapter, and you can specify the type of data to be adapted by generics.

The parameters of the Arrayadapter are as follows:

Android.widget.arrayadapter.arrayadapter<string> (context Context, int Textviewresourceid, string[] objects)

The parameters of the constructor are resolved as follows:

Context: current contextual environment;

int Textviewresourceid : Specifies the ID of the TextView resource contained in the layout file, which is commonly used with Android. R.layout.simple_list_item_1,

Android. R.layout.simple_list_item_1 is an Android built-in layout file with only one TextView for simple display text;

string[] Objects: Text to display

The ListView example is as follows:

(This example is excerpted from the first line of code)

The Android_main.xml code is as follows:

    < ListView          Android:id = "@+id/list_view"         android:layout_width= "Match_parent"        android:layout_height= " Match_parent ">        </ListView>

Because the ListView needs to occupy the entire layout, Match_parent is selected

The Mainactivity.java code is as follows:

 Public classMainactivityextendsActivity {PrivateString[] data={"Apple", "Banana", "Orange", "Watermelon",            "Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango"}; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); //new Array AdapterArrayadapter<string> adapter=NewArrayadapter<string> (mainactivity. This, Android.        R.layout.simple_list_item_1, data); //Load List ViewListView listview=(ListView) Findviewbyid (R.id.list_view); //Pass the built-in adapter object in to create an association between the listview and the DataListview.setadapter (adapter); }}

The operating effect is as follows:

Android notes: ListView and 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.