Android Spinner的ArrayAdapter和SpinnerAdapter注意事项
如图所示:
代码:
package com.example.spinnerdemo;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] start = { "北京", "上海", "成都", "其他" };
String[] target = { "北京", "上海", "成都", "其他" };
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setAdapter(new MyAdapter1(this,
android.R.layout.simple_list_item_1, android.R.id.text1, start));
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner2.setAdapter(new MyAdapter2(this,
android.R.layout.simple_list_item_1, android.R.id.text1, target));
}
private class MyAdapter1 extends ArrayAdapter {
private LayoutInflater infalter;
private String[] start;
private int resource;
private int textViewResourceId;
public MyAdapter1(Context context, int resource,
int textViewResourceId, String[] objects) {
super(context, resource, textViewResourceId, objects);
this.start = objects;
this.resource = resource;
this.textViewResourceId = textViewResourceId;
infalter = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = infalter.inflate(resource, null);
TextView text = (TextView) convertView
.findViewById(textViewResourceId);
text.setText("pos:"+position);
return convertView;
}
@Override
public int getCount() {
return start.length;
}
}
private class MyAdapter2 extends ArrayAdapter {
private LayoutInflater infalter;
private int resource;
private int textViewResourceId;
private String[] target;
public MyAdapter2(Context context, int resource,
int textViewResourceId, String[] objects) {
super(context, resource, textViewResourceId, objects);
this.resource = resource;
this.textViewResourceId = textViewResourceId;
target = objects;
infalter = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = infalter.inflate(resource, null);
TextView text = (TextView) convertView
.findViewById(textViewResourceId);
text.setText("选择了:" + target[position]);
text.setBackgroundColor(Color.GREEN);
return convertView;
}
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
if (convertView == null)
convertView = infalter.inflate(
android.R.layout.simple_list_item_1, null);
TextView text = (TextView) convertView
.findViewById(android.R.id.text1);
text.setText(target[position]);
text.setBackgroundColor(Color.RED);
return convertView;
}
@Override
public int getCount() {
return target.length;
}
}
}
布局文件:
<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.example.spinnerdemo.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>
注意事项:
(1)如果只是使用Android Spinner实现一个表现简单的下拉选项,那么可以直截了当的根据布局文件、数据源构造一个完备的Android Spinner,比如:new ArrayAdapter(this,android.R.layout.simple_list_item_1,android.R.id.text1,data);
new出来的,android Spinner下拉列表中的数据和选中框的数据及view完全一致。这也是Android Spinner最常用的使用场景。
(2)在adapter中,getView负责创建Spinner的选择框选中的item的view呈现,即上图中“pos1”和绿色背景的“选择了:成都”这些view,而getDropDownView则创建下拉选项列表中各个子item的view和数据的创建。两者分工不同,在本例中结合最终的View结合spinner1和spinner2所对应的两个不同adapter代码对比。
在MyAdapter1中,没有实现getDropDownView,但是通过MyAdapter1在构造函数时候接收到完整的资源布局文件、数据源,那么Android自动将下拉列表中的数据从构造中获得的数据渲染下拉列表中的数据。但是由于代码重写了getView,那么选中框将根据getView表现。
(3)使用ArrayAdapter作为Spinner的适配器时候,如果在ArrayAdapter中构造时候不传递过去完整的布局文件、数据源,比如这样构造适配器:new MyArrayAdapter(this,-1),并且不在ArrayAdapter内部实现getDropDownView,则会引起Spinner代码崩溃,原因是因为Spinner会自动加载适配器中的默认构造,进而,如果传递过去的是null,则下拉列表无法正确加载,进而导致代码崩溃。