如何在自定义RecyclerView适配器中使用startActivity(intent)启动新的活动?

7
我有一个CustomRecyclerViewAdapter.class文件,在其中实现了以下方法。
public void onBindViewHolder(RecyclerViewHolder viewHolder, int position)
{
    viewHolder.title.setText(mData.get(position).text);
    //viewHolder.icon.setBackgroundColor(Color.parseColor(mData.get(position).color));

    viewHolder.setClickListener(new RecyclerViewHolder.ClickListener(){

        @Override
        public void onClick(View v, int position, boolean isLongClick) {

            if (isLongClick) {
                // View v at position pos is long-clicked.
                Toast.makeText(v.getContext(), "Hey you just hit item" + position, Toast.LENGTH_SHORT).show();


            }else {
                // View v at position pos is clicked.
                //how to start a new activity here
                Toast.makeText(v.getContext(),"Hey you just hit item" + position,Toast.LENGTH_SHORT).show();
            }


        }
    });

}

所以我该如何在上面的 else 块中启动新活动。
Group.class
public class Group extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);//line no. 16 which is indicated in logcat
        setContentView(R.layout.group);

        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

}

这是我的错误日志记录......
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.trueblueoperator.samplerecyclerview/com.trueblueoperator.samplerecyclerview.Group}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
 Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:151)
        at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
        at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
        at com.trueblueoperator.samplerecyclerview.Group.onCreate(Group.java:16)
        at android.app.Activity.performCreate(Activity.java:5933)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

感谢您的时间。

1
那么目前面临的问题是什么?只需在按钮点击时使用 v.getContext().startActivity(intent) 来启动活动。 - ρяσѕρєя K
1
我使用了以下代码:Intent intent = new Intent(v.getContext(),Group.class); v.getContext().startActivity(intent); - Yogesh Yadav
但是它不幸地停止了应用程序。 - Yogesh Yadav
AndroidManifest.xml 中是否添加了 Group.class?如果是的话,请分享崩溃日志。 - ρяσѕρєя K
我已经在清单文件中添加了类,但仍然遇到相同的问题,并且我已经更新了帖子并附上了崩溃日志。 - Yogesh Yadav
Group类中,尝试继承Activity而不是ActionBarActivity,然后再进行测试。 - ρяσѕρєя K
1个回答

10

您可以将任何视图用作它。getContext()方法提供了几乎所有活动方法,因为Activity扩展了Context。

v.getContext().startActivity(intent);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接