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