我想知道如何使用onItemClickListener
方法在点击列表项时导航到新活动。我知道我们使用Intents,但有人可以提供示例代码吗?
答案 0 :(得分:2)
我没有向listview提供任何适配器来填充它,确保你提供了一些适配器和一些数据,而活动名称也是虚拟的,你也必须定义它们
private static ListView listView;
listView = (ListView) findViewById(R.id.listView);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = null;
switch(position){
case 0:
intent = new Intent(getApplicationContext(), AClassName.class);
break;
case 1:
intent = new Intent(getApplicationContext(), AClassName.class);
break;
case 2:
intent = new Intent(getApplicationContext(), AClassName.class);
break;
case 3:
intent = new Intent(getApplicationContext(), AClassName.class);
break;
case 4:
intent = new Intent(getApplicationContext(), AClassName.class);
break;
case 5:
intent = new Intent(getApplicationContext(), AClassName.class);
break;
default:
intent = new Intent(getApplicationContext(), AClassName.class);
break;
}
if(intent != null){
startActivity(intent);
}
}
});
switch语句中的每个case表示您要打开的其他活动,这样您可以根据列表中单击的项目打开活动
答案 1 :(得分:0)
实现onClickListener时,可以使用v.getContext.startActivity
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.getContext().startActivity(PUT_YOUR_INTENT_HERE);
}
});
答案 2 :(得分:0)
使用mList.setOnItemClickListener(new ListItemClickListener());
private class ListItemClickListener implements OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> listView, View view, int position, long idOfView) {
Intent intent = new Intent(WorkingActivity.this, TargetActivity.class);
startActivity(intent);
}
}
答案 3 :(得分:0)
您需要在listview上使用设置监听器。使用setOnItemClickListener。
mlistView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//Handle the click here
}
});