[Go to] a menu bar for customizing the ArrayAdapter and custom Adapter in Android.
Today, I learned about the Spinner component. Using the Spinner component is equivalent to selecting a project from the drop-down list. The following describes how to use IT (using ArrayAdapter and custom Adapter respectively)
(1): Use ArrayAdapter for data adaptation:
①: First define a layout file:
1 <span style="font-size:16px;"><?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <Spinner 8 android:id="@+id/spinner1" 9 android:layout_width="match_parent"10 android:layout_height="wrap_content"11 />12 </LinearLayout></span>
[Note:] The above spin NER has two attributes: 1. When the prompt is initial, the data displayed by the spin NER is a reference type 2: entries is directly bound to the data source in the xml layout file (you can dynamically bind the data source in the Activity without setting it)
②: Create a data source and use an array. The data will be displayed in the list of the Spinner:
1 <span style = "font-size: 16px;"> <? Xml version = "1.0" encoding = "UTF-8"?> 2 <resources> 3 <string-array name = "spinnername"> 4 <item> Beijing </item> 5 <item> Shanghai </item> 6 <item> Guangzhou </ item> 7 <item> Shenzhen </item> 8 </string-array> 9 </resources> </span>
③: Add the following code to the Activity (the layout file of the drop-down list defined by the system can also be customized)
// Initialize the control mSpinner = (Spinner) findViewById (R. id. spinner1); // create a data source String [] mItems = getResources (). getStringArray (R. array. spinnername); // create an Adapter and bind it to the data source ArrayAdapter <String> _ Adapter = new ArrayAdapter <String> (this, android. r. layout. simple_spinner_item, mItems); // bind the Adapter to the control mSpinner. setAdapter (_ Adapter );
The above code is initially completed. Check the running effect:
The following is a click event (for example) about the Spinner ):
MSpinner. setOnItemSelectedListener (new OnItemSelectedListener () {@ Override public void onItemSelected (AdapterView <?> Parent, View view, int position, long id) {String str = parent. getItemAtPosition (position ). toString (); Toast. makeText (SpinnerActivity. this, "you click:" + str, 2000 ). show () ;}@ Override public void onNothingSelected (AdapterView <?> Parent) {// TODO Auto-generated method stub }});
(2) Use a custom Adapter (important)
①: Define the layout file of each Item
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableLeft="@drawable/ic_launcher" android:paddingRight="8dip" android:paddingTop="8dip" android:text="TextView" android:textSize="25sp" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="8dip" android:paddingTop="8dip" android:text="TextView" android:textSize="25sp" /> </LinearLayout>
②: Create the Person class:
1 package com.jiangqq.csdn; 2 public class Person { 3 private String personName; 4 private String personAddress; 5 public Person(String personName, String personAddress) { 6 super(); 7 this.personName = personName; 8 this.personAddress = personAddress; 9 }10 public String getPersonName() {11 return personName;12 }13 public void setPersonName(String personName) {14 this.personName = personName;15 }16 public String getPersonAddress() {17 return personAddress;18 }19 public void setPersonAddress(String personAddress) {20 this.personAddress = personAddress;21 }22 23 }
③ Create MyAdapter inheritance and BaseAdapter for adaptation:
Package com. jiangqq. csdn; import java. util. list; import android. content. context; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. textView;/*** custom adapter class * @ author jiangqq <a href = http://blog.csdn.net/jiangqq781931404> </a> **/public class MyAdapter extends BaseAdapter {private List <Person> mList; privat E Context mContext; public MyAdapter (Context pContext, List <Person> pList) {this. mContext = pContext; this. mList = pList;} @ Override public int getCount () {return mList. size () ;}@ Override public Object getItem (int position) {return mList. get (position) ;}@ Override public long getItemId (int position) {return position ;}/ *** The following is the important code */@ Override public View getView (int position, view convert View, ViewGroup parent) {LayoutInflater _ LayoutInflater = LayoutInflater. from (mContext); convertView = _ LayoutInflater. inflate (R. layout. item, null); if (convertView! = Null) {TextView _ TextView1 = (TextView) convertView. findViewById (R. id. textView1); TextView _ TextView2 = (TextView) convertView. findViewById (R. id. textView2); _ TextView1.setText (mList. get (position ). getPersonName (); _ TextView2.setText (mList. get (position ). getPersonAddress ();} return convertView ;}}
④: Add the following code to the Activity:
// Initialize the control mSpinner = (Spinner) findViewById (R. id. spinner1); // create a data source List <Person> persons = new ArrayList <Person> (); persons. add (new Person ("Zhang San", "Shanghai"); persons. add (new Person ("Li Si", "Shanghai"); persons. add (new Person ("Wang Wu", "Beijing"); persons. add (new Person ("Zhao ", "Guangzhou"); // create an Adapter to bind the data source MyAdapter _ MyAdapter = new MyAdapter (this, persons); // bind AdaptermSpinner. setAdapter (_ MyAdapter );
The running effect is as follows:
The listening event is the same as the first method: