When using listview, we can directly specify the array resources used in the XML file, or in JavaCodeIs specified through an array. You can also define delimiters when defining listview to make listview more beautiful.
Layout File
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <! -- Directly use array resources to provide list items --> <listview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: entries = "@ array/books" Android: divider = "@ drawable/red" Android: headerdividersenabled = "false"/> <! -- When set to flase, this listview will not draw a separator behind the header view. The default value of this attribute is true. The property value must be set to true or false. --> <! -- Use arrayadapter to provide the list item listview --> <listview Android: Id = "@ + ID/list2" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: divider = "@ drawable/Green"/> </linearlayout>
Array File
<? XML version = "1.0" encoding = "UTF-8"?> <Resources> <string-array name = "books"> <item> similar </item> <item> invisible person </item> <item> tears and poems </item> <item> country of fools </item> </string-array> </resources>
Public class arrayadapterlist extends activity {@ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); listview list2 = (listview) findviewbyid (R. id. list2); // define an array string [] arr = {"Sun Yanzi", "Liang jingru", "avril "}; // wrap the array into arrayadapterarrayadapter <string> arrayadapter = new arrayadapter <string> (this, android. r. layout. simple_list_item_1, arr); // set adapterlist2.setadapter (arrayadapter) for listview );}}