我有一個自定義列表視圖,如果用戶單擊某個項目,我一直在嘗試進入下一個屏幕。它似乎沒有工作。我試過,如果別的,切換。它可以工作,如果我不使用,但這樣,每個項目將會去同一屏幕。任何幫助都會變得渺茫。如何從此ListView中獲取所選項目?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.options_main);
OptionMainHeader option_list[] = new OptionMainHeader[]
{
new OptionMainHeader(R.drawable.clock_icon_48, "Helo"),
new OptionMainHeader(R.drawable.weather_cloudy, "Apple"),
new OptionMainHeader(R.drawable.weather_cloudy, "Mango"),
new OptionMainHeader(R.drawable.weather_cloudy, "Banana"),
};
OptionMainHeader1 adapter = new OptionMainHeader1(this, R.layout.option_main_header1, option_list);
listView1 = (ListView)findViewById(R.id.listView1);
View header = (View)getLayoutInflater().inflate(R.layout.option_main_header, null);
listView1.addHeaderView(header);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
int position = listView1.getSelectedItemPosition();
String selectedFromList =(String) (listView1.getItemAtPosition(position));
if (selectedFromList == "Helo"){
Intent CurrentWaitTime = new Intent(getApplicationContext(), go.class);
startActivity(CurrentWaitTime);
} else if (selectedFromList == "Apple"){
Intent Appointments = new Intent(getApplicationContext(), no.class);
startActivity(Appointments);
} else if (selectedFromList == "Mango"){
Intent Logout = new Intent(getApplicationContext(), go.class);
startActivity(Logout);
} else if (selectedFromList == "Banana"){
Intent Exit = new Intent(getApplicationContext(), low.class);
startActivity(Exit);
}
}
});
}
您無法將字符串與「==」進行比較。你需要做的是更好地使用equals(String) – gian1200
@ gian1200方法,他應該將字符串放在string.xml文件中,並使用enums作爲值。這樣他也可以使用開關箱,並減少代碼編寫。 –
'getItemAtPosition()'不應該返回一個'OptionMainHeader'?如果它返回一個'OptionMainHeader',是不是你的轉換String selectedFromList =(String)(listView1.getItemAtPosition(position));'wrong?你可以發佈LogCat消息來檢查崩潰的原因嗎? –