ListView Page Layout: Layout/activity_main.xml:
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical" >
<tablelayout
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:stretchcolumns= "1" >
<tablerow >
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Select Year"
android:layout_gravity= "Center_vertical"/>
<spinner
Android:id= "@+id/spyears"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:prompt= "@string/prompt_year"///1, set default display, 2,prompt [PRM (P) t] "Computer" prompt information
Android:spinnermode= "Dialog"//Setting drop-down style
/>
</TableRow>
</TableLayout>
</LinearLayout>
Java code in this layout:
Package com.example.day05_0222;
Import android.support.v7.app.ActionBarActivity;
Import android.content.res.Resources;
Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.view.View;
Import Android.widget.AdapterView;
Import Android.widget.AdapterView.OnItemSelectedListener;
Import Android.widget.ArrayAdapter;
Import Android.widget.BaseAdapter;
Import Android.widget.Spinner;
Import Android.widget.Toast;
public class Mainactivity extends Actionbaractivity {
Private Spinner spyear;//down the Marquee object
Private Baseadapter Arrayadapter;//Adapter
Private String [] spyearnames;//Data array
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Init ();
InitData ();
Setlistener ();
}
private void InitData () {//Initialize data
TODO auto-generated Method Stub
Resources resource = Getresources ();
Spyearnames = Resource.getstringarray (r.array.years);
}
private void Setlistener () {//Set Listener events after selection
TODO auto-generated Method Stub
Spyear.setonitemselectedlistener (New Onitemselectedlistener () {
@Override
public void onitemselected (adapterview<?> parent, view view, int position,
Long id) {
TODO auto-generated Method Stub
Switch (Parent.getid ()) {
Case R.id.spyears:
String year = spyearnames[position];
Toast.maketext (Mainactivity.this, "The selected year is:" +year, 20000). Show ();
Break
}
}
@Override
public void onnothingselected (adapterview<?> parent) {
TODO auto-generated Method Stub
}
});
}
private void init () {
TODO auto-generated Method Stub
//Get down the Marquee object
Spyear = (Spinner) Findviewbyid (r.id.spyears);
//Get Adapter Object
The first parameter is the activity object where the layout is to be displayed, the second parameter is the physical address of the collection data to be displayed, and the third parameter is
refers to the appropriate layout page, where Android comes directly with the layout
Arrayadapter = Arrayadapter.createfromresource (this, R.array.years, Android. R.layout.simple_list_item_1);
Setting up the adapter
Spyear.setadapter (Arrayadapter);
}
}
String string resource: Values/strings.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<string name= "App_name" >Day05_0222</string>
<string name= "Hello_world" >hello world!</string>
<string name= "Action_settings" >Settings</string>
<string name= "Prompt_year" > select year </string>
<string-array name= "Years" >
<item>2000</item>
<item>2001</item>
<item>2002</item>
<item>2003</item>
<item>2004</item>
<item>2005</item>
<item>2006</item>
<item>2007</item>
</string-array>
</resources>
Effect:
650) this.width=650; "src=" http://img.blog.csdn.net/20141130103951394 "/>
This article from "Follow the heart of the other side" blog, declined reprint!
The Arrayadaper of Android spinner