我有一個listView與列表中的2個項目,第一個位置0項目響應點擊,但其他不!我試圖複製相同的onclick方法,並將if語句更改爲位置1中的列表項,但它不適合我,所以我正在尋找如何讓這個工作,如果有人可以幫助我?我已經包括了唯一的代碼,我認爲是需要解決這個問題,問題打開列表視圖項目與意圖在Android工作室
public class TopLevelActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_top_level);
//Create an OnItemClickListener
AdapterView.OnItemClickListener itemClickListener =
new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> listView,
View v,
int position,
long id) {
if (position == 0) {
Intent intent = new Intent(TopLevelActivity.this,
DrinkCategoryActivity.class);
startActivity(intent);
}
}
};
//Add the listener to the list view
ListView listView = (ListView) findViewById(R.id.list_options);
listView.setOnItemClickListener(itemClickListener);
//what ive tried to open the foodCategoryActivity list item
AdapterView.OnItemClickListener itemClickListener =
new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> listView,
View v,
int position,
long id) {
if (position == 1) {
Intent intent = new Intent(TopLevelActivity.this,
FoodCategoryActivity.class);
startActivity(intent);
}
}
};
//Add the listener to the list view
ListView listView = (ListView) findViewById(R.id.list_options);
listView.setOnItemClickListener(itemClickListener);
}
}
謝謝。
啊所以它的情況下,如果其他塊然後說,例如我有8項我應該讓他們嵌套如果語句 – james
你只需複製我的代碼?你在做什麼? – breakline
@breakline:是的,但我只是複製代碼,而不是我在答案中寫的文本。好吧,現在看到我的答案,這是不同的codewise也 –