2014-03-31 50 views
0

我有一個微調器可以顯示兩個選項,但是當我接受女性選項時,它仍然將男性的答案作爲答案,有什麼建議嗎?微調器數據接受是多功能的

List<String> SpinnerArray = new ArrayList<String>(); 
    SpinnerArray.add("Male"); 
    SpinnerArray.add("Female"); 

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, SpinnerArray); 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    Spinner Items = (Spinner) findViewById(R.id.genderSpinner); 
    Items.setAdapter(adapter); 

    // Importing all assets like buttons, text fields 
    inputFullName = (EditText) findViewById(R.id.registerName); 
    String selected = Items.getSelectedItem().toString(); 
    if (selected.equals("Male")) { 
     inputGen = "Male"; 
    }  
    if (selected.equals("Female")){ 
     inputGen = "Female"; 
    } 
+0

你有沒有考慮過使用'getSelectedItemPosition()'?然後'if(selected == 0){.....}'和'else if(selected == 1){.....}' – EdmDroid

回答

1

official guide

public class SpinnerActivity extends Activity implements OnItemSelectedListener { 
    ... 

public void onItemSelected(AdapterView<?> parent, View view, 
     int pos, long id) { 
    // An item was selected. You can retrieve the selected item using 
    // parent.getItemAtPosition(pos) 
} 

public void onNothingSelected(AdapterView<?> parent) { 
    // Another interface callback 
} 

你可以嘗試positionswitch,然後嘗試你的條件相同。