2014-02-05 21 views
0

我有一個包含兩個spinners並實現OnItemSelectedListener的活動。要找出選擇的項目,我使用一個開關。當一個項目被選中時,我有一個簡單的吐司來告訴用戶選擇了什麼。問題是當活動創建時,吐司會出現,並說明選擇了哪個項目。我只想在用戶選擇除默認項目之外的東西時顯示敬酒,即創建活動時顯示的東西。這是我下面帶有OnItemSelectedListener的多個Spinners問題

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    SpinnerAdapterTexts sat = new SpinnerAdapterTexts(); 

    final Spinner spin = (Spinner)findViewById(R.id.spinner); 
    ArrayAdapter<String> arrayAd = new ArrayAdapter<String>(this,R.layout.row,R.id.type,sat.getType()); 
    spin.setAdapter(arrayAd); 
    spin.setOnItemSelectedListener(this); 

    final Spinner spinService = (Spinner)findViewById(R.id.spinnerService); 
    ArrayAdapter<String> arrayAd2 = new ArrayAdapter<String>(this,R.layout.row,R.id.type, sat.getServices()); 
    spinService.setAdapter(arrayAd2); 
    spinService.setOnItemSelectedListener(this); 

} 

@Override 
public void onItemSelected(AdapterView<?> parent, View view, int pos , 
     long id) { 
    // TODO Auto-generated method [email protected] 

    switch(parent.getId()) { 
     case R.id.spinner: 
      selectedType = parent.getItemAtPosition(pos).toString(); 

      Toast.makeText(getApplicationContext(), selectedType + " was selected", Toast.LENGTH_LONG).show(); 

      break; 
     case R.id.spinnerService: 
      selectedType = parent.getItemAtPosition(pos).toString(); 

      Toast.makeText(getApplicationContext(), selectedType + " was selected", Toast.LENGTH_LONG).show(); 
      break; 
    } 

    selectedType = parent.getItemAtPosition(pos).toString(); 
    Log.e("TAG", selectedType); 

} 

@Override 
public void onNothingSelected(AdapterView<?> arg0) { 
    // TODO Auto-generated method stub 

} 
+0

假設你的默認項目是在列表中的位置1要這樣做 如果(selectedType.equalsIgnoreCase(「defaultItem」)) 吐司。 makeText(getApplicationContext(),selectedType +「被選中」,Toast.LENGTH_LONG).show(); –

+0

用戶選擇默認項目是什麼?這意味着他們打開微調器,然後選擇默認項目。我仍然希望顯示,但不是在創建活動時顯示。 –

回答

0

代碼,然後做這個

int count; 
protected void onCreate(){ 
    count=0; 
    //your codes 
} 
//then before toast 
if(count==0){ 
    count=1; 
    Toast(the user selection); 
}