我正在創建一個android微調器。它顯示正常,但沒有任何反應選擇一個選項。爲什麼'if'命令不起作用?如何從android微調獲取文本?
public class Main2Activity extends AppCompatActivity {
Spinner spinner;
ArrayAdapter<CharSequence> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
spinner= (Spinner) findViewById(R.id.spinner);
adapter=ArrayAdapter.createFromResource(this,R.array.spinner,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {//why 'if' command is not working?
if(adapterView.getItemAtPosition(i).toString().equals("Question 1"))
{
Intent intent=new Intent(Main2Activity.this,Main2Activity.class);
}
if(adapterView.getItemAtPosition(i).toString().equals("Question 2"))
{
Intent intent=new Intent(Main2Activity.this,main3Activity.class);
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});`
您是否嘗試過調試以查看'adapterView.getItemAtPosition(i)'正在返回什麼?使用'adapter.get(i).toString()。equals(「Question 1」)''會更清楚更好。或者,如果您知道這些項目的順序,只需針對問題1提供問題1的「if(i == 0)」和問題2的「如果(i == 1)」。 –