Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start new activity using startActivity(intent) in Custom RecyclerView Adapter?

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

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 i can start the 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 error logcat.....

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)

Thanks for your time.

like image 767
Yogesh Yadav Avatar asked Jun 24 '15 16:06

Yogesh Yadav


People also ask

How many times onCreateViewHolder is called in RecyclerView?

By default it have 5. you can increase as per your need. Save this answer.

What is viewType in onCreateViewHolder?

onCreateViewHolder(parent: ViewGroup, viewType: Int) Parent is the ViewGroup into which the new View will be added after it is bound to an adapter position and viewType is the type of the new View. This method will return a new ViewHolder that holds a View of the given view type.


1 Answers

You can use any View to it. getContext() method provides almost activity methods because Activity extends Context.

v.getContext().startActivity(intent);
like image 96
garciparedes Avatar answered Oct 12 '22 14:10

garciparedes