Android study note 19: listview

Source: Internet
Author: User

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 );}}
Related Article

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.