2015-06-26 67 views
-3

值未顯示在微調器中 當點擊微調器時,我獲取了值,下拉列表中顯示了值。 但不是當我選擇微調微調器中未顯示數值Android

vechType_spinner.setOnItemSelectedListener(new OnItemSelectedListener() 
     { 

      @Override 
      public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) 
      { 
       if (position != 0) 
       { 

        Toast.makeText(CreateItinerary.this, "testing vehicle", Toast.LENGTH_LONG).show(); 
        selectedVehicalType = vechiclelist.get(position); 

        if (selectedVehicalType.contains("Car") || selectedVehicalType.contains("Motor Cycle") || selectedVehicalType.contains("Bike")) 
        { 
         expectedKM.setVisibility(View.VISIBLE); 
         //classSpinner.setVisibility(View.GONE); 
        } 

        else 
        { 
         expectedKM.setVisibility(View.GONE); 
        } 




       } 
       else 
       { 
        Toast.makeText(CreateItinerary.this, "testing else vehicle", Toast.LENGTH_LONG).show(); 
        expectedKM.setVisibility(View.GONE); 
        //classSpinner.setVisibility(View.GONE); 

       } 
      } 

      @Override 
      public void onNothingSelected(AdapterView<?> arg0) 
      { 
       Toast.makeText(CreateItinerary.this, "testing NOthing selected vehicle", Toast.LENGTH_LONG).show(); 
      } 
     }); 

onItemSelected是沒有得到調用。 雖然項目得到下拉? 是由於XML重疊?

回答

0

嘗試像這樣在你的代碼,使OnitemSelelected變化將工作

 @Override 
      public void onItemSelected(AdapterView<?> parent, View view, int position, 
        long id) 
{ 
       String name = parent.getItemAtPosition(position).toString(); 

       //Do What ever you want 

         if (selectedVehicalType.contains("Car") || selectedVehicalType.contains("Motor Cycle") || selectedVehicalType.contains("Bike")) 
         { 
          expectedKM.setVisibility(View.VISIBLE); 
          //classSpinner.setVisibility(View.GONE); 
         } 

         else 
         { 
          expectedKM.setVisibility(View.GONE); 
         } 

      } 

裏面添加的onCreate

spinner = (Spinner) findViewById(R.id.spinner2); 
     ArrayAdapter<String> adapter_state2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, YourStringArray); 
     adapter_state2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     spinner .setAdapter(adapter_state2); 
     spinner .setOnItemSelectedListener(this);