标签:
mainActivity
1 package com.zzw.spinner; 2 3 import android.app.Activity; 4 import android.content.Context; 5 import android.os.Bundle; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.widget.AdapterView; 10 import android.widget.AdapterView.OnItemSelectedListener; 11 import android.widget.ArrayAdapter; 12 import android.widget.Spinner; 13 import android.widget.TextView; 14 import android.widget.Toast; 15 16 public class MainActivity extends Activity { 17 18 private String[] start_citys, end_citys; 19 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.activity_main); 24 25 start_citys = getResources().getStringArray(R.array.start_city); 26 end_citys = getResources().getStringArray(R.array.end_city); 27 28 Spinner spinner1 = (Spinner) findViewById(R.id.spinner1); 29 spinner1.setAdapter(new MySpinnerAdapter(this, R.layout.item_start, R.id.TextView_start, start_citys)); 30 Spinner spinner2 = (Spinner) findViewById(R.id.spinner2); 31 spinner2.setAdapter(new MySpinnerAdapter(this, R.layout.item_end, R.id.TextView_end, end_citys)); 32 33 spinner1.setOnItemSelectedListener(new OnItemSelectedListener() { 34 @Override 35 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 36 Toast.makeText(getApplicationContext(), start_citys[position], 0).show(); 37 } 38 39 @Override 40 public void onNothingSelected(AdapterView<?> parent) { 41 42 } 43 }); 44 45 spinner2.setOnItemSelectedListener(new OnItemSelectedListener() { 46 47 @Override 48 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 49 Toast.makeText(getApplicationContext(), end_citys[position], 0).show(); 50 } 51 52 @Override 53 public void onNothingSelected(AdapterView<?> parent) { 54 55 } 56 }); 57 58 } 59 60 private class MySpinnerAdapter extends ArrayAdapter { 61 private Context context; 62 private int item; 63 private int R_textView; 64 private String[] citys; 65 LayoutInflater inflater; 66 67 public MySpinnerAdapter(Context context, int resource, int textViewResourceId, String[] citys) { 68 super(context, resource, textViewResourceId, citys); 69 this.context = context; 70 this.item = resource; 71 this.R_textView = textViewResourceId; 72 this.citys = citys; 73 inflater = LayoutInflater.from(context); 74 } 75 76 @Override 77 public int getCount() { 78 return citys.length; 79 } 80 81 // 下拉框中显示的view 82 @Override 83 public View getView(int position, View convertView, ViewGroup parent) { 84 if (convertView == null) { 85 convertView = inflater.inflate(item, null); 86 } 87 TextView textView = (TextView) convertView.findViewById(R_textView); 88 textView.setText(citys[position]); 89 return convertView; 90 } 91 92 // 显示的是下拉的列表view 93 // 如果数据没有从new的时候传递下来,那么不写这个方法有Resources$NotFoundException异常 94 @Override 95 public View getDropDownView(int position, View convertView, ViewGroup parent) { 96 if (convertView == null) { 97 convertView = inflater.inflate(item, null); 98 } 99 TextView textView = (TextView) convertView.findViewById(R_textView); 100 textView.setText(citys[position]); 101 return convertView; 102 } 103 104 } 105 }
xml:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="horizontal" 6 tools:context="com.zzw.spinner.MainActivity" > 7 8 <Spinner 9 android:id="@+id/spinner1" 10 android:layout_weight="1" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 /> 14 <Spinner 15 android:id="@+id/spinner2" 16 android:layout_weight="1" 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" 19 /> 20 </LinearLayout>
标签:
原文地址:http://www.cnblogs.com/zzw1994/p/4975468.html