【问题标题】:Using an Intent in a list onItemClick在 onItemClick 列表中使用 Intent
【发布时间】:2011-08-23 17:31:15
【问题描述】:

我正在进行我的第二个活动(主要),如下所示:

Login -> Main -> Vforum

我设法在登录活动中使用 Intent 进入主活动:

Intent logMeIn = new Intent(this,Main.class);
startActivity(logMeIn);

效果很好。我现在的问题是从 Main 转到 Vforum。

projectList.setOnItemClickListener(new OnItemClickListener()
{
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        Intent launchVforum = new Intent(this, Vforum.class);
        startActivity(launchVforum);
    }
});

projectList 是 ListView。 Eclipse 说:

The constructor Intent(new AdapterView.OnItemClickListener(){}, Class<Vforum>) is undefined

我不知道在this 的位置可以修复它。我只想去我的第三个活动(Vforum)。

【问题讨论】:

    标签: android listview click android-intent


    【解决方案1】:

    是的。曾经有过类似的问题。我的解决方案是执行以下操作(使用您的示例):

    -在您的主要活动中放置一个私有上下文,如下所示:

    private Context mCtx;
    

    -在您的主要活动 onCreate() 方法中将这一行放在某处:

    mCtx = this;
    

    -创建意图时使用 mCtx 代替:

    Intent launchVforum = new Intent(mCtx, Vforum.class);
    

    【讨论】:

    • 解释一下,问题是第一个参数需要是一个Context对象。如果您不想保存上下文,也可以尝试 Main.this
    【解决方案2】:
    projectList.setOnItemClickListener(new OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
            Intent launchVforum = new Intent(YourActivity.this, Vforum.class);
            startActivity(launchVforum);
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多