Android component------List selection box

Source: Internet
Author: User
Tags xml attribute

The list selection box (Spinner) provided by Android is equivalent to the selection dropdown box for web-side user registration, such as registering for a selected province city. If it is a list selection box

The list selection for the drop-down list can be specified by the Android:entries property of the XML file, or imported in Java code, and the attribute android:prompt is the title of the list item.

A list item data:

in practice, the data of many drop-down list items is actually known and can be placed in XML resource files. At this point, the developer can specify the data through the XML attribute.

In addition to resource files, developers can also use adapters to adapt data sources. (Adapter: If your computer does not receive a Bluetooth signal, you can install a Bluetooth adapter to receive the Bluetooth signal.) The function is to change the signal or data that does not conform to its own form)

Two about listening

After the user chooses the drop-down list, the program needs to be processed accordingly, which requires the listener. The listener for the list selection box is Onitemselectedlistener (), and the developer rewrites the onitemselected () method.

Three case operations

1. Create a list selection box for a resource file configuration

1> New project, add spinner in Layout, specify attribute android:entries as data

2> create an XML resource file, name Array.xml, add an array of strings to the file, and the data

3> add monitoring to spinner. There are two methods for listening, which are the options after selection and not

4> Run program, can see drop-down list, and have log output

2. Data adaptation with adapter

Specifying a data adapter for a drop-down list typically requires three steps:

1. Create a data adapter, generally using Arrayadapter. There are two ways to create a common use:

1> created from an array resource file

The 2>java code is created from a string array

2. Set list drop-down selection style

3. Add the adapter to the drop-down list

Here's how:

1> Create a build adapter and set a drop-down style through a resource file

        arrayadapter<charsequence> adapter = Arrayadapter.createfromresource (This, R.array.data,                Android. R.layout.simple_dropdown_item_1line);                 = (Spinner) Findviewbyid (r.id.spinner1);                S.setadapter (adapter);

2> Creating and setting a drop-down style from a string array

        New string[]{"China", "Czech", "Japan", "Nanjing", "Chongqing", "Chengdu"};                 ArrayadapterNew arrayadapter<string> (This,                Android. R.layout.simple_dropdown_item_1line,data);                 = (Spinner) Findviewbyid (r.id.spinner1);                S.setadapter (adapter);

Summary: Actually said so much, this section spinner the use of drop-down box to focus on the drop-down box data settings: The use of XML to specify the data source resource file, or adapter adaptation. There are two definitions of an adapter, either reading a resource file or creating an adapter in its own definition.

the level is limited, the insufficient place please leave a message!

Android component------List selection box

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.