Android Learning notes ListView

Source: Internet
Author: User

In the actual application of mobile phone, we often need a list to display our information, such as our contact list, SMS information list and so on. Implemented via the ListView in Android.

The XML property of the ListView

Property name

Describe

Android:choicemode

Specifies the selection mode used by this ListView. By default, thelist has no selection mode. The property value must be set to one of the following constants:

None, with a value of 0, indicating no selection mode;

Singlechoice, a value of 1, indicates that a maximum of one item can be selected;

Multichoice, a value of 2, indicates that multiple items can be selected.

android:divider

Rules list items are separated by a graphic or color. Can you use

android:dividerheight

Delimiter height. If no height is specified, the height that is intrinsic to this delimiter is used. Must be a floating-point number with units, such as px (pixel pixels), DP (, SP (scaled pixels based on Preferred font size  is based on a fixed proportion of the font size of pixels),  in (inches ").

You can use "@[package: ]type:name"? [ Package:][type:]name (Theme Property) is formatted to point to a resource that contains this type of value.

Android:entries

A reference to an array that will be used in this listview. If the array is fixed, using this property will be simpler than writing in the program.

Must be "@[+][package:]type:name" or "? [ Package:][type:]name "In the form of" to point to a resource. "

Android:footerdividersenabled

When set to Flase, this ListView will not draw a delimiter in front of the footer view. The default value for this property is true.

The property value must be set to true or false.

Can you use "@[package:]type:name" or "? [ Package:][type:]name (Theme Property) is formatted to point to a resource that contains this type of value.

Android:headerdividersenabled

When set to Flase, this ListView will not draw a delimiter after the header view. The default value for this property is true.

The property value must be set to true or false.

Can you use "@[package:]type:name" or "? [ Package:][type:]name (Theme Property) is formatted to point to a resource that contains this type of value.

Example one: Using the list format that comes with AndroidXML layout file
<?XML version= "1.0" encoding= "Utf-8"?> <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"Android:paddingleft= "8DP"Android:paddingright= "8DP">          <ListViewAndroid:id= "@id/android:list"Android:layout_width= "Fill_parent"Android:layout_height= "0dip"Android:background= "#FFFFFF"Android:divider= "#000000"Android:dividerheight= "2DP"Android:layout_weight= "1"Android:drawselectorontop= "false"/>          <TextViewAndroid:id= "@id/android:empty"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"Android:background= "#FF0000"Android:text= "No Data"/>"</LinearLayout>
Source:
@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); String[] Contries=getresources (). Getstringarray (R.array.countries_arry);  Setcontentview (R.layout.activity_listview); Setlistadapter (NewArrayadapter<string> ( This, Android.  r.layout.simple_list_item_multiple_choice,contries)); Getlistview (). Setchoicemode (Listview.choice_mode_multiple); //Here we set the selection mode directly in the source code, or we can configure it in the XML file.Getlistview (). SetBackgroundColor (Color.White); Getlistview (). Setonitemclicklistener (NewOnitemclicklistener () {@Override Public voidOnitemclick (adapterview<?> arg0, view view,intposition,LongID) {Toast.maketext (Getapplicationcontext (), ((TextView) view). GetText (), Toast.length_short). Show (); }  }); }
There are three selection modes for the ListView: Choice_mode_none: Indicates no selection mode, the item item cannot be selected when set to No selection mode, but responds to the ItemClick event Choice_mode_ Single: Indicates that there can be at most one of the selected Choice_mode_multiple: Indicates that the data behind the ListView can be set setlistadapter multiple options. The parameter is ListAdapter, where we use Arrayadapter, which adatper can specify our own defined Item object. Here I use the existing object of the system: Android. R.layout.simple_list_item_single_choice General: Simple_list_item_single_choice The corresponding choice mode is: Choice_mode_sigle; (effect one) Simple_list_item_multiple_choice corresponding selection mode is: choice_mode_multiple (effect two) simple_list_item_1 corresponding selection Mode: Choice_mode_none (effect three) custom ListView format to create an XML file that describes the list item format
 <?  xml version= "1.0" encoding= "Utf-8"  ?>  <  textview  xmlns:android  = "Http://schemas.android.com/apk/res/android"   Android:layout_width  = "Match_parent"   Android:layout_height  = "Match_parent"   Android:paddingleft  = "2DP"   Android:paddingright  = "2DP"  >  </ textview  >  
source File Settings XML file
@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); String[] Contries=getresources (). Getstringarray (R.array.countries_arry); Setlistadapter (NewArrayadapter<string> ( This, R.layout.activity_mylistview, contries)); Getlistview (). Setonitemclicklistener (NewOnitemclicklistener () {@Override Public voidOnitemclick (adapterview<?> arg0, view view,intPositionLongID) {Toast.maketext (Getapplicationcontext (), ((TextView) view). GetText (), Toast.length_short). Show (); }  }); }

In addition, it uses the definition of the array string: defined in the Values/string.xml file as follows:

    <String-arrayname= "Countries_arry">        <Item>Bharain</Item>        <Item>Bangladesh</Item>        <Item>Barbados</Item>        <Item>Belarus</Item>        <Item>Belgium</Item>        <Item>Belize</Item>        <Item>Benin</Item>    </String-array>
This makes it possible to reference the toast in the source code through R.array.countries_arry:

A toast is a view that provides concise information to the user. the Toast class helps you create and display this information.

The view is rendered to the user in a form that floats above the application. Because it does not get the focus, even if the user is entering nothing will be affected. Its goal is to be as unobtrusive as possible in a way that enables users to see the information you provide. There are two examples of volume control and settings information saved successfully.

The simplest way to use this class is to call a static method and let him construct everything you need and return a new Toast object.

Constant

int Length_long

Persistent display of views or text prompts for a longer period of time. The length of the time can be customized.

See

setDuration(int)

int Length_short

Displays the view or text prompt continuously for a short time. The length of the time can be customized. The value is the default value.

See

setDuration(int)

Android Learning notes ListView

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.