How do I start a new activity using startActivity (intent) in a custom RecyclerView adapter?

I have a CustomRecyclerViewAdapter.class file in which I have implemented the method below.

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();
            }


        }
    });

}

      

So how can I start a new activity in the else block above.

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);

}

      

This is my logcat error .....

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)

      

Thank you for your time.

+3


source to share


1 answer


You can use any kind. The getContext () method provides almost methods of the activity as the Activity extends the context.



v.getContext().startActivity(intent);

      

+5


source







All Articles