我现在要实现一个二级联动的下拉列表,就是选定省份后,城市的下拉列表出现相应变化,但一直报错,请大家帮我看看
package zye.client.Client;import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;public class cityset extends Activity{
private String[] province = new String[] {"直辖市", "特别行政区","黑龙江"};
private String[] city = new String[]{"北京","上海","天津","重庆"};
private String[][] pandc = new String[][]{{"北京","上海","天津","重庆"},{"香港","澳门"},{"哈尔滨","齐齐哈尔","牡丹江","大庆","伊春","双鸭山","鹤岗","鸡西","佳木斯","七台河","黑河","绥化","大兴安岭"}};
private Spinner sp;
private Spinner sp2;
private Context context;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cityset);
context = this;
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, province);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp = (Spinner) findViewById(R.id.province);
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(selectListener);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, city);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp2 = (Spinner) findViewById(R.id.city);
sp2.setAdapter(adapter2);
}
private OnItemSelectedListener selectListener = new OnItemSelectedListener(){
public void onItemSelected(AdapterView parent, View v, int position,long id){
int pos = sp.getSelectedItemPosition();
adapter2 = new ArrayAdapter<String>(context,android.R.layout.simple_spinner_item, pandc[pos]);
sp2.setAdapter(adapter2);
}
public void onNothingSelected(AdapterView arg0){ } };}<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
>
<Spinner android:id="@+id/province"
android:layout_width="fill_parent"
android:layout_height="45px"
android:layout_marginLeft="75px"
android:layout_marginRight="20px"
android:layout_marginTop="7px"
android:drawSelectorOnTop="true"
android:layout_alignParentRight="true"
/>
<Spinner android:id="@+id/city"
android:layout_width="fill_parent"
android:layout_height="45px"
android:layout_marginLeft="75px"
android:layout_marginRight="20px"
android:layout_marginTop="3px"
android:drawSelectorOnTop="true"
android:layout_alignParentRight="true"
android:layout_below="@id/province"
/>
</RelativeLayout>
package zye.client.Client;import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;public class cityset extends Activity{
private String[] province = new String[] {"直辖市", "特别行政区","黑龙江"};
private String[] city = new String[]{"北京","上海","天津","重庆"};
private String[][] pandc = new String[][]{{"北京","上海","天津","重庆"},{"香港","澳门"},{"哈尔滨","齐齐哈尔","牡丹江","大庆","伊春","双鸭山","鹤岗","鸡西","佳木斯","七台河","黑河","绥化","大兴安岭"}};
private Spinner sp;
private Spinner sp2;
private Context context;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cityset);
context = this;
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, province);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp = (Spinner) findViewById(R.id.province);
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(selectListener);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, city);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp2 = (Spinner) findViewById(R.id.city);
sp2.setAdapter(adapter2);
}
private OnItemSelectedListener selectListener = new OnItemSelectedListener(){
public void onItemSelected(AdapterView parent, View v, int position,long id){
int pos = sp.getSelectedItemPosition();
adapter2 = new ArrayAdapter<String>(context,android.R.layout.simple_spinner_item, pandc[pos]);
sp2.setAdapter(adapter2);
}
public void onNothingSelected(AdapterView arg0){ } };}<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
>
<Spinner android:id="@+id/province"
android:layout_width="fill_parent"
android:layout_height="45px"
android:layout_marginLeft="75px"
android:layout_marginRight="20px"
android:layout_marginTop="7px"
android:drawSelectorOnTop="true"
android:layout_alignParentRight="true"
/>
<Spinner android:id="@+id/city"
android:layout_width="fill_parent"
android:layout_height="45px"
android:layout_marginLeft="75px"
android:layout_marginRight="20px"
android:layout_marginTop="3px"
android:drawSelectorOnTop="true"
android:layout_alignParentRight="true"
android:layout_below="@id/province"
/>
</RelativeLayout>
public void onItemSelected(AdapterView parent, View v, int position,long id){
int pos = sp.getSelectedItemPosition();
ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(context,android.R.layout.simple_spinner_item, pandc[pos]);
sp2.setAdapter(adapter3);
}
public void onNothingSelected(AdapterView arg0){ } };又重新声明了一个ArrayAdapter,不过还是不明白为什么不能重用adapter2,另外怎么省份和城市的下拉列表的表现形式不一样,那个城市的下拉列表打开颜色非常地暗,省份的下拉列表就很好
public void onItemSelected(AdapterView parent, View v, int position,long id){
int pos = sp.getSelectedItemPosition();
adapter2 = new ArrayAdapter<String>(context,android.R.layout.simple_spinner_item, pandc[pos]);
sp2.setAdapter(adapter2);
}
这里有错!
// ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, city);
// adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp2 = (Spinner) findViewById(R.id.city);
// sp2.setAdapter(adapter2);
只要第三句,就行了,剩下的adapter2在private OnItemSelectedListener selectListener = new OnItemSelectedListener(){}里面赋值就行了