在 Android 应用程序的 ListView 中按下某个项目时调用 Activity
当我按下 ListView 中的某个项目时,我想调用我在 Android 应用程序中编写的活动之一。 我想使用的代码是这样的:
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
.
.
.
Intent lp = new Intent(this, myFirst.class);
startActivity(lp);
}
});
但是,我收到与以下两行相关的错误:
Intent lp = new Intent(this, myFirst.class);
startActivity(lp);
基本上,当按下列表视图上的项目时,我想调用“myFirst”活动。 在这里调用“myFirst”活动的正确方法是什么?
感谢您的帮助。
泰杰
I would like to call one of the activities that I have written in my android app, when I press an item in a ListView.
The code that I want to use is this:
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
.
.
.
Intent lp = new Intent(this, myFirst.class);
startActivity(lp);
}
});
However it I get error related to the following two lines:
Intent lp = new Intent(this, myFirst.class);
startActivity(lp);
basically I want to call "myFirst" activity when an item on my list view is pressed.
What is the correct way of calling "myFirst" activity here?
Thanks for the help.
TJ
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您收到此错误是因为您将
OnItemClickListener
实例传递给您的意图。尝试传递
Actity
的实例You are getting this error because you are passing an instance of
OnItemClickListener
to your intent.Try to pass the instance of
Actiity
我想我可以在这方面帮助你。 “此”可能不是指您当前的活动。
使用以下内容代替“这个”。
NameOfTheActivityThatIsCurrentlyOpen.this(即文件名.this)。这总是有效的。
I think I can help you in this. 'this' may not be refering to your current activity.
Instead of 'this', use the following.
NameOfTheActivityThatIsCurrentlyOpen.this(i.e., the filename.this). This always works.
你可以这样做
或者
you can do like this way
or