答案 0 :(得分:3)
你必须像以下那样设置适配器:
package com.example.mapsdemo;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class MainActivity extends Activity {
private Spinner spin1;
private Spinner spin2;
ArrayList<String> a = new ArrayList<String>();
ArrayList<String> b = new ArrayList<String>();
ArrayList<String> c = new ArrayList<String>();
// private ImageView imageView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spin1 = (Spinner) findViewById(R.id.spinner1);
spin2 = (Spinner) findViewById(R.id.spinner2);
fillarray();
spin1.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_dropdown_item_1line, a));
/*
* spin2.setAdapter(new ArrayAdapter<String>(MainActivity.this,
* android.R.layout.simple_dropdown_item_1line, b));
*/
spin1.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int pos,
long arg3) {
// TODO Auto-generated method stub
if (pos == 0) {
spin2.setAdapter(new ArrayAdapter<String>(
MainActivity.this,
android.R.layout.simple_dropdown_item_1line, b));
} else {
spin2.setAdapter(new ArrayAdapter<String>(
MainActivity.this,
android.R.layout.simple_dropdown_item_1line, c));
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
private void fillarray() {
// TODO Auto-generated method stub
a.clear();
a.add("a");
a.add("b");
a.add("c");
a.add("d");
a.add("e");
b.clear();
b.add("1");
b.add("2");
b.add("3");
b.add("4");
c.clear();
c.add("Android");
c.add("ios");
}
}
您的布局文件如:
<RelativeLayout 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" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="40dp" />
<Spinner
android:id="@+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/spinner1"
android:layout_marginTop="80dp" />
</RelativeLayout>
答案 1 :(得分:3)
可能会有所帮助,
ArrayAdapter<String> secondspinnerAdapter;
ArrayAdapter<String> firstspinnerAdapter = new ArrayAdapter<String>(
MainActivity.this, android.R.layout.simple_spinner_item,
firstspinnervalue);
first_spinner.setAdapter(firstspinnerAdapter);
firstspinnerAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
firstspinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
// get data from database add to arraylist
secondspinnerAdapter = new
ArrayAdapter<String>( mainActivity.this,
android.R.layout.simple_spinner_item, arraylistvalue);
second_spinner.setAdapter(secondspinnerAdapter);
second_spinner.setSelection(arg2, false);
secondspinnerAdapter.setDropDownViewResource
(android.R.layout.simple_spinner_dropdown_item);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
选择第一个微调器项,然后从数据库中获取数据并填充arraylist并设置secondspinner。
同样遵循选择的第二个微调器项目。