Two Methods for loading data in the Spinner drop-down list

Source: Internet
Author: User

In Android, there are two common methods to load data for the Spinner component:
I. Program to load list data
:
 
First, add the spinner component to the layout file main. xml. The Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Spinner_1"
/>
<Spinner
Android: id = "@ + id/spinner_1"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>
</LinearLayout>
Then, in MainActivity. java, the custom method add_list () is used to load the data of the Spinner. The Code is as follows:
Public class MainActivity extends Activity {
Private static final String [] mCountries = {"China", "USA", "Japan ",
"Russia", "Canada "};
Spinner sp = null;
 
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Add_list ();
}
 
Private void add_list (){
Sp = (Spinner) findViewById (R. id. spinner_1 );
ArrayAdapter <String> adapter = new ArrayAdapter <String> (this,
Android. R. layout. simple_spinner_item, mCountries );
Adapter. setDropDownViewResource (android. R. layout. simple_spinner_dropdown_item );
Sp. setAdapter (adapter );
}
}
2. Pre-define list data in XML
First, define the list data in strings. xml. The Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<String name = "hello"> Hello World, MainActivity! </String>
<String name = "app_name"> SpinnerTest </string>

<String-array name = "countries">
<Item> China </item>
<Item> USA </item>
<Item> Japan </item>
<Item> Russia </item>
<Item> Canada </item>
</String-array>
</Resources>
Second, add the spinner component to the layout file main. xml. The Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Spinner_1"
/>
<Spinner
Android: id = "@ + id/spinner_1"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>
</LinearLayout>
Finally, in MainActivity. java, run the following code to load the data of the Spinner:
Public class MainActivity extends Activity {
Spinner sp = null;
 
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Sp = (Spinner) findViewById (R. id. spinner_1 );
ArrayAdapter <CharSequence> adapter = ArrayAdapter. createFromResource (
This, R. array. countries,
Android. R. layout. simple_spinner_dropdown_item );
Sp. setAdapter (adapter );
}
}
 
This article is from the "Android Learning Experience" blog

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.