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