首先是MainActivity
package com.example.spinnertest;import java.util.ArrayList;import java.util.List;import android.os.Bundle;import android.app.Activity;import android.content.Context;import android.text.TextUtils;import android.view.LayoutInflater;import android.view.Menu;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.ArrayAdapter;import android.widget.Spinner;import android.widget.TextView;import com.example.spinnertest.R;public class MainActivity extends Activity { private Spinner myS; private Listdatas=new ArrayList (); private MySpinnerAdapter sAdapter; private String value; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myS = (Spinner)findViewById(R.id.spinnerS); initData(); sAdapter = new MySpinnerAdapter(this,datas); myS.setAdapter(sAdapter); refreshShowView(); myS.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub value = sAdapter.getItem(arg2); } @Override public void onNothingSelected(AdapterView arg0) { // TODO Auto-generated method stub } }); } private void initData() { // TODO Auto-generated method stub datas.add("a"); datas.add("b"); datas.add("c"); datas.add("d"); datas.add("e"); datas.add("f"); value = datas.get(4); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } class MySpinnerAdapter extends ArrayAdapter { private List mList; private Context mContext; class ViewHolder { public TextView spinnerTv; public ViewHolder(View v){ spinnerTv = (TextView)v.findViewById(R.id.spinner_tv); } } public MySpinnerAdapter(Context context, List list) { super(context,R.layout.spinner_item,R.id.spinner_tv, list); // TODO Auto-generated constructor stub setData(list); mContext = context; } public void setData(List list){ mList = list; notifyDataSetChanged(); } @Override public int getCount() { // TODO Auto-generated method stub if(mList==null){ return 0; } return mList.size(); } @Override public String getItem(int position) { // TODO Auto-generated method stub if(mList==null){ return null; } return mList.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getDropDownView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if(convertView==null){ convertView = LayoutInflater.from(mContext).inflate( R.layout.spinner_item, null); holder= new ViewHolder(convertView); convertView.setTag(holder); }else{ holder = (ViewHolder)convertView.getTag(); } String myListItem = mList.get(position); holder.spinnerTv.setText(myListItem); return convertView; } } private void refreshShowView(){ if(!TextUtils.isEmpty(value)){ int i = 0; for(String item:datas){ if(value.equals(item)){ break; } i++; } if(i==datas.size()){ i = datas.size()-1; } myS.setSelection(i); } };}
然后是activity_main.xml
然后是spinner_item: