根据另一个Spinner更改Spinner选项

时间:2013-07-23 15:50:04

标签: java android android-arrayadapter android-spinner

我正在尝试使用微调器在我的数据库中实现用户搜索。

我有车队和车辆(车队包含车辆)。我有一个车队列表作为一个旋转器和一个车辆列表作为另一个。

默认情况下,我希望车队设置为“全部”,车辆设置为显示所有车辆(目前情况如此),但是当车队输入更改为特定车辆时,例如车队1,车辆微调器应该相应更新[这将通过SQLite数据库搜索实现,但我不认为问题在这里]。

如何更改fleet微调器数据时如何设置侦听器?

vehicleSpinner = (Spinner) findViewById(R.id.vehicleSpinner); 
      String selected = (String)fleetSpinner.getSelectedItem();
      ArrayAdapter<String> adapter5 = null;
      if(selected == "All"){
          //show all vehicles
          adapter5 = new ArrayAdapter<String>(this, R.layout.sherlock_spinner_item, vehicleArrayListString);

  }else{
      String vehiclesInFleetQuery = "SELECT * FROM " + Database.TABLE_VEHICLE + " WHERE " + Database.COLUMN_FLEET + "='" + selected +"'";
      Log.i(TAG,"QUERY: "+ vehiclesInFleetQuery);
      Cursor cursor = Database.listOfVehiclesDesired(query);


        if(cursor.moveToFirst()){
            do {
             String addToList = cursor.getString(cursor.getColumnIndex(Database.COLUMN_VEHICLE_ID));
             vehicleArrayFleet.add(addToList);
            } while (cursor.moveToNext());
        }else{//error on fleet search, no vehicles in fleet
            vehicleArrayFleet = vehicleArrayListString;
            builderContinue.setMessage("Selected Fleet(" + selected + ") had zero associated vehicles").setTitle("Error").show();
        }


        if (cursor != null && !cursor.isClosed()) {
            System.out.println("Closed");
            cursor.close();
        }

       adapter5 = new ArrayAdapter<String>(this, R.layout.sherlock_spinner_item, vehicleArrayFleet);
  }


 adapter5.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
  vehicleSpinner.setAdapter(adapter5);

1 个答案:

答案 0 :(得分:1)

您需要为fleet微调器设置OnItemSelectedListener。您可以在http://start-jandroid.blogspot.com/2011/01/android-spinner-example.html找到示例。从车队微调器中的侦听器,您可以设置车辆微调器的选定项目。