我在listview中有五个项目和五个活动,现在我想打开 单击listview的单个项目我的单个活动意味着Actvity1应该 单击item1时打开,单击item2时应打开Activity2, 单击item3时应打开Activity3。 请告诉我我该怎么做。
谢谢
答案 0 :(得分:2)
您可以根据onItemClick
项目的位置OnItemClickListener
ListView
方法在 ListView listView=(ListView) findViewById(R.id.listView);
listView.setOnItemClickListener(new AdapterView.onItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
if (position == 0) {
Intent intent = new Intent(YourActivityName.this, Activity1.class);
startActivity(intent);
}
else if (position == 1) {
Intent intent = new Intent(YourActivityName.this, Activity2.class);
startActivity(intent);
}
else if (position == 2) {
Intent intent = new Intent(YourActivityName.this, Activity3.class);
startActivity(intent);
}
else if (position == 3) {
Intent intent = new Intent(YourActivityName.this, Activity4.class);
startActivity(intent);
}
else if (position == 4) {
Intent intent = new Intent(YourActivityName.this, Activity5.class);
startActivity(intent);
}
}
});
方法中添加条件来实现:
public Telnet(Parcel in) {
readFromParcel(in);
}
答案 1 :(得分:1)
您可以从OnItemClickListener获取位置,如下所示:
ListView lv=(ListView) findViewById(R.id.listview);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// position 0 -> activity one
}
});
或者您可以为每一行分配一个标记值,并使用view.getTag();
进行检查希望这会有所帮助。