[英]List view opens the same activity
我已成功調用某項目的活動。 但是問題是我已經添加了一個括號,但是即使在列表視圖的其他項目上,它仍然調用這兩個活動。
mainListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(position==0);
{
Intent i = new Intent(Lessons1.this, ForthActivity.class);
startActivity(i);
}
if(position==1);
Intent i = new Intent(Lessons1.this, ForthActivity.class);
startActivity(i);
}
});
}
}
切勿在if語句后添加分號。 這個
if(position==0);
{
// some stuff
}
相當於
if(position==0)
{
// do nothing
}
{
// some stuff
}
這意味着無論position
值如何,您總是會做“一些事情”。
這個
if(position==1);
Intent i = new Intent(Lessons1.this, ForthActivity.class);
startActivity(i);
}
應該
if(position==1){
Intent i = new Intent(Lessons1.this, ForthActivity.class);
startActivity(i);
}
新:
mainListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position==0){
Intent i = new Intent(Lessons1.this, ForthActivity.class);
startActivity(i);
}
if(position==1){
Intent i = new Intent(Lessons1.this, ForthActivity.class);
startActivity(i);
}
}
});
順便說一句,您注意到您正在呼叫同一活動
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.