The drop-down list is a spinner. This is easy to use. Define a tag in xml and specify the ArrayAdapter <String>,
You must specify the drop-down layout for the Adapter. For the drop-down data, the book uses static arrays. I changed it to read from the resource file.
Java code
Package zziss. android. spinnerTest;
Import java. util. Map;
Import android. app. Activity;
Import android. content. res. Resources;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. AdapterView;
Import android. widget. ArrayAdapter;
Import android. widget. Spinner;
Import android. widget. TextView;
Public class SpinnerTestActivity extends Activity {
/** Called when the activity is first created .*/
Private TextView iTv;
Private Spinner iSpinner;
Private String [] items;
Private ArrayAdapter <Map <String, Integer> iAda;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
ITv = (TextView) this. findViewById (R. id. TV );
ISpinner = (Spinner) this. findViewById (R. id. spinner );
Resources res = getResources ();
Items = res. getStringArray (R. array. listItems );
ArrayAdapter <String> ada = new ArrayAdapter <String> (this,
Android. R. layout. simple_spinner_item, items );
Ada. setDropDownViewResource (android. R. layout. simple_spinner_dropdown_item );
ISpinner. setAdapter (ada );
ISpinner. setOnItemSelectedListener (new Spinner. OnItemSelectedListener ()
{
@ Override
Public void onItemSelected (AdapterView <?> Arg0, View arg1,
Int arg2, long arg3 ){
// TODO Auto-generated method stub
ITv. setText (items [arg2]);
}
@ Override
Public void onNothingSelected (AdapterView <?> Arg0 ){
// TODO Auto-generated method stub
}
});
}
}
Main. xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<TextView
Android: id = "@ + id/TV"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = ""/>
<Spinner
Android: id = "@ + id/spinner"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_centerHorizontal = "true"
/>
</LinearLayout>
String. xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<String name = "hello"> Hello World, SpinnerTestActivity! </String>
<String name = "app_name"> SpinnerTest </string>
<String-array name = "listItems">
<Item> type A </item>
<Item> type B </item>
<Item> AB </item>
<Item> O-type </item>
</String-array>
</Resources>