Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Spinner to implement a list selection box in Android

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

The knowledge of this article "how to use Spinner to achieve a list selection box in Android" is not understood by most people, so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use Spinner to achieve a list selection box in Android" article.

Application of Android Spinner list selection box

Spinner is the list selection box for Android, but spinner does not need to display a drop-down list, but is equivalent to popping up a menu for users to choose from.

Spinner attribute:

● android:spinnerMode: the mode in which the list is displayed, with two choices, a pop-up list (dialog) and a drop-down list (dropdown), or a drop-down list if not specifically set.

● android:entries: use resources to configure data sources.

● android:prompt: sets the title for the current drop-down list, valid only in dialog mode. Pass a "@ string/name" resource that needs to be defined in the resource file.

Spinner Common events:

● AdapterView.OnItemCLickListener: triggered when a list item is clicked.

● AdapterView.OnItemLongClickListener: list items are triggered on time by a long time.

● AdapterView.OnItemSelectedListener: triggered when a list item is selected.

Spinner data binding:

● uses xml to set up array resources, which is simple and convenient, but not flexible, and can be used if only displaying fixed simple data.

● uses adapter interface settings, the most common way, dynamic loading flexibility, you can set a variety of styles and data sources.

XML file structure (usually written in string.xml file)

Item1 item2 item3

For simple data, you can use ArrayAdapter to set up the adapter, but for slightly complex data, ArrayAdapter can not meet the requirements, so you need to use SimpleAdapter to adapt.

The SimpleAdapter adapter is also based on Adapter, which can map static layout files in XML format to the view. You can specify data in List format. The data in list corresponds to every row in Spinner, and the data in Map corresponds to no data mSpinner = (Spinner) findViewById (R.id.sp_bank).

MSpinner = (Spinner) findViewById (R.id.sp_bank); banklist = (List) resultMap.get ("list"); / / Connect options to ArrayAdapter adapter = new ArrayAdapter (self, R.layout.simple_spinner_item, banklist); / / set the style of the drop-down list adapter.setDropDownViewResource (android.R.layout.simple_spinner_dropdown_item); / / add adapter to spinner mSpinner.setAdapter (adapter) / / add event Spinner event listener mSpinner.setOnItemSelectedListener (new SpinnerSelectedListener ()); class SpinnerSelectedListener implements OnItemSelectedListener {public void onItemSelected (AdapterView parent, View view, int position, long id) {String str = parent.getItemAtPosition (position). ToString (); / / Toast.makeText (self, "you clicked:" + str, 2000). Show (); bankname = str;} public void onNothingSelected (AdapterView arg0) {}}

The Spinner pop-up list event is listview, so various methods of listview can be used here in the Spinner pop-up window, and you can also customize the data source at that time, customizing Adapter inherits BaseAdapter to achieve more flexible and complex effects.

Spinner automatically calls an OnItemSelectedListener event when initializing, because the system automatically loads default values.

Solution: manually add the default value, which requires the main call order, which must be called before registering the method.

/ / called before event registration, generally specify the 0th as the default spinner.setSelection (0, true). The above is about "how to use Spinner to achieve a list selection box in Android". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report