【问题标题】:What should I pass to this Intent constructor inside an event listener?我应该在事件侦听器中将什么传递给这个 Intent 构造函数?
【发布时间】:2021-02-19 22:29:30
【问题描述】:

我是 Android 开发的新手,当用户单击 ListView 中的项目时,我正在尝试启动另一个活动。但是,我一直试图创建一个 Intent 来做到这一点。我正在尝试遵循this 指南,他们在其中使用this 关键字。但是,在这种情况下它不起作用,因为this 指的是AdapterView.onItemClickListener

我当前的代码是这样的:

list.setOnItemClickListener(
   new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> adapter,
                              View item, int pos, long id) {
         Intent intent = new Intent(this, OtherActivity.class);
        // use the Intent to start another activity...
        }
   });

我应该用什么替换this

【问题讨论】:

    标签: java android android-intent this


    【解决方案1】:

    this之前使用您的活动名称,例如:

    Intent intent = new Intent(MainActivity.this, OtherActivity.class);
    

    【讨论】:

      猜你喜欢
      • 2022-01-16
      • 2015-12-07
      • 2015-09-24
      • 1970-01-01
      • 1970-01-01
      • 2010-09-12
      • 2011-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多