2011-05-26 38 views
1

如果有效,將會是第一個應用程序。 約有290項。打開一個新的頁面被點擊。但錯誤後的「開關」或「如果」超過250個。可能是什麼原因?android listview

if (position == 0) { 
    Intent newActivity = new Intent(this, otobus5.class); 
    startActivity(newActivity); 
} else if (position == 1) { 
    Intent newActivity6 = new Intent(this, otobus6.class); 
    startActivity(newActivity6); 
} 
. 
. 
. 
if (position == 290) { 
    Intent newActivity = new Intent(this, otobus290.class); 
    startActivity(newActivity); 
} else if (position == 291) { 
    Intent newActivity6 = new Intent(this, otobus291.class); 
    startActivity(newActivity6); 
} 
+2

我不明白你要做什麼,但有290如果聲明肯定是錯誤的。解釋你真正的問題,我們可以給你一個更好的解決方案。 – Kaj 2011-05-26 18:06:39

+0

一般提示,如果使用:-),則使用開關而不使用其他開關。你的問題還不清楚,你可以重述嗎? – 2011-05-26 18:08:54

+4

親愛的上帝,請告訴我你真的沒有290個不同的班級... – kcoppock 2011-05-26 18:12:04

回答

3

這些類包含什麼,它們如何變化?最有可能的,你可以有一個單一的活動(otobus,在這種情況下),並且簡單地將位置作爲一個額外的,就像這樣:

Intent newActivity = new Intent(this, otobus.class); 
newActivity.putIntExtra("position", position); 
startActivity(newActivity); 

然後,在你otobus活動的onCreate(),簡單地處理佈局活動的基礎上通過的立場:

Intent passedIntent = getIntent(); 
int selectedPosition = passedIntent.getExtra("position", -1); 
+1

謝謝。工作:) – rhymes 2011-05-27 12:47:39