Recently doing a project for Android Spinner use, here is a simple example to test how to use.
Spinner
is a drop-down list, drag a spinner control into the Android interface and set android:entries= "@array/spinner_data" in the property
Where Spinner_data is the array set in string. The value in the array that is written by the fix. At this point, you can set the Drop-down list in the interface.
<spinner
android:id= "@+id/spinner1"
android:layout_width= "Wrap_content"
Wrap_content "
android:layout_alignleft=" @+id/textview1 "
android:layout_below=" @+id/textview1
" android:layout_margintop= "17DP"
android:entries= "@array/spinner_data"/>
Of course for the user experience good, you can set the Android:spinnermode= "dialog" property, you can make the Drop-down list has a pop-up box.
Adapter:
The adapter is primarily responsible for fetching data from the data source and transmitting it to individual UI components. The main adapters are adapter by Arrayadapter and Baseadapter. That is, the adapter is responsible for getting data from a data source such as an array to spinner, which is displayed on the interface.
First define the data source
Private string[] ss=new string[]{
"Cat",
"Tiger",
"Lion",
};
Data Source 2
Private list<string> list=new arraylist<string> ();
List.add ("Baidu");
List.add ("Tencent");
List.add ("Alibaba");
Second, define a class Myadapter inheritance and abstract class Baseadapter, there are four abstract methods.
Private class Myadapter extends baseadapter{
@Override public
int getcount</span> () {
//TODO auto-generated method stub return
ss.length;
}
@Override public
Object getitem (int position) {
//TODO auto-generated method stub return
null;
}
@Override public
long getitemid (int position) {
//TODO auto-generated method stub return
0;
}
@Override public
View getview</span> (int position, View Convertview, ViewGroup parent) {
//TODO auto-generated method Stub
TextView textview=new TextView (mainactivity.this);
Textview.settext (Ss[position]);
return textView;
}
And then again in the main function
Spinner spinner= (Spinner) Findviewbyid (r.id.spinner1);
Baseadapter adapter=new myadapter ();
Spinner.setadapter (adapter);
First locate the spinner control and instantiate the new one adapter. The adapter adapter is used to crawl data from the data source SS.
The most important method for Baseadapter adapters is the GetCount () and the GetView () method. The former returns the length of the data source, and the latter carries out some operations.
Of course, you can use a simpler arrayadaper adapter.
Spinner spinner= (Spinner) Findviewbyid (r.id.spinner1);
Arrayadapter<string> adapter=new arrayadapter<string> (this,android. R.LAYOUT.SIMPLE_SPINNER_ITEM,SS);
Spinner.setadapter (adapter);
With the above statement, you can locate directly in the array SS.
Thank you for reading, I hope to help you, thank you for your support for this site!