如何点击listView的item跳转到指定的Activity
1个回答
展开全部
当我们在做demo时,为了简化代码,常用listView来做,当我们点击listView的item时,跳转到某个activity测试相应功能,而主界面的listview就封装了每个activity的入口,
我们都知道点击listView的某个item让他弹出一个toast,我们如何点击item让他跳转到一个activity呢?其实也差不多,关键是得把每个activity转成xxxActivity.class,然后通过
intent和startActivity()进行跳转即可,完整代码如下:
[java] view plain copy
//重写listView的onItemClick方法
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getActivity(), demos[position].demoClass);
startActivity(intent);
}
//把每个activity转成class
private static class DemoInfo {
private final Class<? extends android.app.Activity> demoClass;
public DemoInfo(Class<? extends android.app.Activity> demoClass) {
this.demoClass = demoClass;
}
}
//把每个activity转成xxx.class
private static final DemoInfo[] demos = {
new DemoInfo(FirstGameActivity.class),
new DemoInfo(SecondGameActivity.class),
new DemoInfo(ThirdGameActivity.class),
new DemoInfo(FourthGameActivity.class),
new DemoInfo(FifthGameActivity.class),
new DemoInfo(SixthGameActivity.class)
};
这样,即可实现点击listView的item跳转到指定activity的功能.
我们都知道点击listView的某个item让他弹出一个toast,我们如何点击item让他跳转到一个activity呢?其实也差不多,关键是得把每个activity转成xxxActivity.class,然后通过
intent和startActivity()进行跳转即可,完整代码如下:
[java] view plain copy
//重写listView的onItemClick方法
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getActivity(), demos[position].demoClass);
startActivity(intent);
}
//把每个activity转成class
private static class DemoInfo {
private final Class<? extends android.app.Activity> demoClass;
public DemoInfo(Class<? extends android.app.Activity> demoClass) {
this.demoClass = demoClass;
}
}
//把每个activity转成xxx.class
private static final DemoInfo[] demos = {
new DemoInfo(FirstGameActivity.class),
new DemoInfo(SecondGameActivity.class),
new DemoInfo(ThirdGameActivity.class),
new DemoInfo(FourthGameActivity.class),
new DemoInfo(FifthGameActivity.class),
new DemoInfo(SixthGameActivity.class)
};
这样,即可实现点击listView的item跳转到指定activity的功能.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询