简单Spinner

mainActivity

 package com.zzw.spinner;

 import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
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 android.widget.Toast; public class MainActivity extends Activity { private String[] start_citys, end_citys; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); start_citys = getResources().getStringArray(R.array.start_city);
end_citys = getResources().getStringArray(R.array.end_city); Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setAdapter(new MySpinnerAdapter(this, R.layout.item_start, R.id.TextView_start, start_citys));
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner2.setAdapter(new MySpinnerAdapter(this, R.layout.item_end, R.id.TextView_end, end_citys)); spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), start_citys[position], 0).show();
} @Override
public void onNothingSelected(AdapterView<?> parent) { }
}); spinner2.setOnItemSelectedListener(new OnItemSelectedListener() { @Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), end_citys[position], 0).show();
} @Override
public void onNothingSelected(AdapterView<?> parent) { }
}); } private class MySpinnerAdapter extends ArrayAdapter {
private Context context;
private int item;
private int R_textView;
private String[] citys;
LayoutInflater inflater; public MySpinnerAdapter(Context context, int resource, int textViewResourceId, String[] citys) {
super(context, resource, textViewResourceId, citys);
this.context = context;
this.item = resource;
this.R_textView = textViewResourceId;
this.citys = citys;
inflater = LayoutInflater.from(context);
} @Override
public int getCount() {
return citys.length;
} // 下拉框中显示的view
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(item, null);
}
TextView textView = (TextView) convertView.findViewById(R_textView);
textView.setText(citys[position]);
return convertView;
} // 显示的是下拉的列表view
// 如果数据没有从new的时候传递下来,那么不写这个方法有Resources$NotFoundException异常
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(item, null);
}
TextView textView = (TextView) convertView.findViewById(R_textView);
textView.setText(citys[position]);
return convertView;
} }
}

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="horizontal"
tools:context="com.zzw.spinner.MainActivity" > <Spinner
android:id="@+id/spinner1"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Spinner
android:id="@+id/spinner2"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
上一篇:[java] 虚拟机(JVM)底层结构详解[转]


下一篇:JVM 内部原理(五)— 基本概念之 Java 虚拟机官方规范文档,第 7 版