Android list selection box spinner usage details

The list selection box (spinner) provided by Android is equivalent to the selection drop-down box when web users register, such as provinces and cities to be selected for registration. The following figure is a list selection box

The list selection item of the drop-down list can be specified through the Android: entries attribute of the XML file or imported in Java code. The Android: prompt attribute is the title of the list item.

List item data

In practice, the data of many drop-down list items are actually known and can be placed in XML resource files. At this time, developers can specify data through XML attributes.

In addition to resource files, developers can also use adapters to adapt 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 signals or data that do not match your own form)

II. About monitoring

After the user selects from the drop-down list, the program needs to carry out corresponding processing, which requires a listener. The listener for the list selection box is onitemselectedlistener(), and the developer overrides the onitemselected() method.

III. case operation

1. Create a list selection box for resource file configuration

1) Create a new project, add spinner in the layout, and specify the attribute Android: entries as data

2) Create an XML resource file, name array.xml, and add a string array named data to the file

3) . add listening to spinner. There are two methods for monitoring, namely, the method after selection and the method without selection

4) Run the program, you can see the drop-down list, and there is log output

2. Data adaptation through adapter

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

1. Create a data adapter, generally using arrayadapter. There are usually two methods for creating:

1) Create through array resource file

2) , created by string array in Java code

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

3. Add the adapter to the drop-down list

The specific operations are as follows:

1) Create and build an adapter through the resource file and set the drop-down style

2) Create and set the drop-down style through the string array

Summary: after all, the use of the spinner drop-down box in this section focuses on the setting of the drop-down box data: specify the data source resource file with XML or adapter adaptation. There are also two definitions of adapters: reading resource files or creating adapters by defining arrays.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>