我对Android开发非常新,我正在尝试制作一个互相继电器的mutli微调器,我已经成功地在另一个上进行了一次继电器,但是我不能在第二次继电器上进行第三次继电器,也不能在第二次继电器上继电器第三
感谢您的帮助
这是我的XML
<Spinner
android:id="@+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dip"
android:layout_marginRight="8dip"
android:layout_marginTop="10dip"
android:entries="@array/Type" />
<Spinner
android:id="@+id/spinner2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:layout_marginLeft="8dip"
android:layout_marginRight="8dip"/>
<Spinner
android:id="@+id/spinner3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:layout_marginLeft="8dip"
android:layout_marginRight="8dip"/>
<Spinner
android:id="@+id/spinner4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:layout_marginLeft="8dip"
android:layout_marginRight="8dip"
这是我的java
public class VehicleProfile extends Activity implements AdapterView.OnItemSelectedListener {
Spinner s1,s2, s3, s4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vehicle_profile);
s1 = (Spinner)findViewById(R.id.spinner1);
s2 = (Spinner)findViewById(R.id.spinner2);
s3 = (Spinner)findViewById(R.id.spinner3);
s4 = (Spinner)findViewById(R.id.spinner4);
s1.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
String sp1= String.valueOf(s1.getSelectedItem());
Toast.makeText(this, sp1, Toast.LENGTH_SHORT).show();
if(sp1.contentEquals("List-1")) {
List<String> list = new ArrayList<String>();
list.add("Select Model");
list.add("Alfa Romeo");
list.add("Audi");
list.add("BMW");
list.add("Chevrolet");
list.add("Citroen");
list.add("Dacia");
list.add("Ford");
list.add("Honda");
list.add("hyundai");
list.add("Jaguar");
list.add("KIA");
list.add("KTM");
list.add("Land Rover");
list.add("Mazda");
list.add("Mercedes-Benz");
list.add("Mini Cooper");
list.add("Nissan");
list.add("Opel");
list.add("Peugeot");
list.add("Porsche");
list.add("Renault");
list.add("Saab");
list.add("SEAT");
list.add("Skoda");
list.add("Volvo");
list.add("VW");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter.notifyDataSetChanged();
s2.setAdapter(dataAdapter);
}
if(sp1.contentEquals("List-2")) {
List<String> list = new ArrayList<String>();
list.add("Select Model");
list.add("Alfa Romeo");
list.add("Audi");
list.add("BMW");
list.add("Cadillac");
list.add("Chevrolet");
list.add("Chrysler");
list.add("Citroen");
list.add("Dacia");
list.add("Daewoo");
list.add("Dodge");
list.add("Fiat");
list.add("Ford");
list.add("Honda");
list.add("Hymer");
list.add("hyundai");
list.add("Infiniti");
list.add("Isuzu");
list.add("Iveco");
list.add("Jaguar");
list.add("Jeep");
list.add("KIA");
list.add("Lancia");
list.add("Land Rover");
list.add("Lexus");
list.add("Maserati");
list.add("Mazda");
list.add("Mercedes-Benz");
list.add("MG");
list.add("MG(Rover)");
list.add("Mini Cooper");
list.add("Mitsubishi");
list.add("Nissan");
list.add("Opel");
list.add("Peugeot");
list.add("Porsche");
list.add("Renault");
list.add("Rover");
list.add("Saab");
list.add("SEAT");
list.add("Skoda");
list.add("Smart");
list.add("SsangYong");
list.add("Subaru");
list.add("Suzuki");
list.add("Toyota");
list.add("Volvo");
list.add("VW");
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter2.notifyDataSetChanged();
s2.setAdapter(dataAdapter2);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
我试过在[public void onNothingSelected(AdapterView arg0){]之前设置这个2函数,但它没有用,我可以看到页面中的spiners,但它们是空的,无法打开
String sp2= String.valueOf(s2.getSelectedItem());
Toast.makeText(this, sp2, Toast.LENGTH_SHORT).show();
if(sp2.contentEquals("Alfa Romeo")) {
List<String> list = new ArrayList<String>();
list.add("Select Engine");
list.add("145 1.9 JTD 8V");
list.add("146 1.9 JTD 8V");
ArrayAdapter<String> dataAdapter3 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter3.notifyDataSetChanged();
s3.setAdapter(dataAdapter3);
}
String sp3= String.valueOf(s3.getSelectedItem());
Toast.makeText(this, sp3, Toast.LENGTH_SHORT).show();
if(sp3.contentEquals("145 1.9 JTD 8V")) {
List<String> list = new ArrayList<String>();
list.add("Select Engine");
list.add("77 kw, 105 hp, 255 nm");
list.add("146 1.9 JTD 8V");
ArrayAdapter<String> dataAdapter4 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter4.notifyDataSetChanged();
s3.setAdapter(dataAdapter4);
}
答案 0 :(得分:0)
微调器2和3不会触发onItemSelected你应该添加:
s2.setOnItemSelectedListener(this);
s3.setOnItemSelectedListener(this);
像这样编辑你的代码:
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
if (arg1.getId()==R.id.spnnier1){
String sp1= String.valueOf(s1.getSelectedItem());
Toast.makeText(this, sp1, Toast.LENGTH_SHORT).show();
if(sp1.contentEquals("List-1")) {
List<String> list = new ArrayList<String>();
list.add("Select Model");
list.add("Alfa Romeo");
list.add("Audi");
list.add("BMW");
list.add("Chevrolet");
list.add("Citroen");
list.add("Dacia");
list.add("Ford");
list.add("Honda");
list.add("hyundai");
list.add("Jaguar");
list.add("KIA");
list.add("KTM");
list.add("Land Rover");
list.add("Mazda");
list.add("Mercedes-Benz");
list.add("Mini Cooper");
list.add("Nissan");
list.add("Opel");
list.add("Peugeot");
list.add("Porsche");
list.add("Renault");
list.add("Saab");
list.add("SEAT");
list.add("Skoda");
list.add("Volvo");
list.add("VW");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter.notifyDataSetChanged();
s2.setAdapter(dataAdapter);
}
if(sp1.contentEquals("List-2")) {
List<String> list = new ArrayList<String>();
list.add("Select Model");
list.add("Alfa Romeo");
list.add("Audi");
list.add("BMW");
list.add("Cadillac");
list.add("Chevrolet");
list.add("Chrysler");
list.add("Citroen");
list.add("Dacia");
list.add("Daewoo");
list.add("Dodge");
list.add("Fiat");
list.add("Ford");
list.add("Honda");
list.add("Hymer");
list.add("hyundai");
list.add("Infiniti");
list.add("Isuzu");
list.add("Iveco");
list.add("Jaguar");
list.add("Jeep");
list.add("KIA");
list.add("Lancia");
list.add("Land Rover");
list.add("Lexus");
list.add("Maserati");
list.add("Mazda");
list.add("Mercedes-Benz");
list.add("MG");
list.add("MG(Rover)");
list.add("Mini Cooper");
list.add("Mitsubishi");
list.add("Nissan");
list.add("Opel");
list.add("Peugeot");
list.add("Porsche");
list.add("Renault");
list.add("Rover");
list.add("Saab");
list.add("SEAT");
list.add("Skoda");
list.add("Smart");
list.add("SsangYong");
list.add("Subaru");
list.add("Suzuki");
list.add("Toyota");
list.add("Volvo");
list.add("VW");
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter2.notifyDataSetChanged();
s2.setAdapter(dataAdapter2);
}
}
else
if (arg1.getId()==R.id.spinner2){
String sp2= String.valueOf(s2.getSelectedItem());
Toast.makeText(this, sp2, Toast.LENGTH_SHORT).show();
if(sp2.contentEquals("Alfa Romeo")) {
List<String> list = new ArrayList<String>();
list.add("Select Engine");
list.add("145 1.9 JTD 8V");
list.add("146 1.9 JTD 8V");
ArrayAdapter<String> dataAdapter3 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter3.notifyDataSetChanged();
s3.setAdapter(dataAdapter3);
}
else
if (arg1.getId()==R.id.spinner3){
String sp3= String.valueOf(s3.getSelectedItem());
Toast.makeText(this, sp3, Toast.LENGTH_SHORT).show();
if(sp3.contentEquals("145 1.9 JTD 8V")) {
List<String> list = new ArrayList<String>();
list.add("Select Engine");
list.add("77 kw, 105 hp, 255 nm");
list.add("146 1.9 JTD 8V");
ArrayAdapter<String> dataAdapter4 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter4.notifyDataSetChanged();
s3.setAdapter(dataAdapter4);
}
}