Android components ------ list selection box, Android components ------ list

Source: Internet
Author: User
Tags xml attribute

Android components ------ list selection box, Android components ------ list

The list selection box provided by Android is equivalent to the selection drop-down box for web terminal user registration, such as the province and city for registration. Such as a list selection box

The selected items in the drop-down list can be specified through the android: entries attribute of the xml file or imported in java code. The property android: prompt is the title of the list item.

A list of item data:

In actual use, the data of many drop-down lists is actually known and can be stored in xml resource files. In this case, the developer can specify data through the xml Attribute.

In addition to resource files, developers can also use adapters to adapt to data sources. (Adapter: if your computer cannot receive Bluetooth signals, you can install a Bluetooth adapter to receive Bluetooth signals. The function is to convert a signal or data that does not match your own form)

 

2. Listener

After you select a listener from the drop-down list, the program needs to process the listener accordingly. The listener of the list selection box is OnItemSelectedListener (). The developer overrides the OnItemSelected () method.

Case 3

1. Create a resource file configuration list selection box

1> Create a project, add a Spinner to the layout, and specify the property android: entries as Data

 

2> Create an xml resource file named array. xml. Add another string array named Data to the file.

 

3> Add a listener to the Spinner. There are two methods in the listener: The method after selection and the method without selection.

4> run the program to view the drop-down list and log output.

 

2. data adapter

It usually takes three steps to specify a data adapter for the drop-down list:

1. Create a data adapter. Generally, ArrayAdapter is used. Two methods are usually used for creation:

1> Create an array resource file

2> Create A String Array in java code

2. Select a style from the drop-down list.

3. Add the adapter to the drop-down list

The procedure is as follows:

1> Create a license using the resource file and set the drop-down Style

        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.Data,                android.R.layout.simple_dropdown_item_1line);                Spinner s = (Spinner)findViewById(R.id.spinner1);                s.setAdapter(adapter);

 

2> Create and set the drop-down style through the String Array

String [] data = new String [] {"China", "Czech", "Japan", "Nanjing", "Chongqing", "Chengdu "}; arrayAdapter <String> adapter = new ArrayAdapter <String> (this, android. r. layout. simple_dropdown_item_1line, data); Spinner s = (Spinner) findViewById (R. id. spinner1); s. setAdapter (adapter );

 

Summary: in fact, this section, the use of the Spinner drop-down box is nothing more than the settings of the data in the drop-down box: use xml to specify the data source resource file, or adapter adaptation. There are two definitions for the adapter: Read the resource file, or define an array to create an adapter.

The level is limited. Leave a message if you have any shortcomings!

 

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.