Start Activity from RecycleView Adapter on click

2019-06-09 16:28发布

I have an issue starting a new activity from a CardView Adapter, this is the code:

RVAdapter adapter = new RVAdapter(array_restaurants);
recList.setAdapter(adapter);

And after in the adapter. I set an OnClickListener

personName.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                Context context = v.getContext();
                System.out.println("Context");
                System.out.println(context.toString());
                Intent intent = new Intent(v.getContext(), Restaurante.class);
                v.getContext().startActivity(intent);
            }
        });

When i print the context in the console everything looks fine, but after the aplication stop working. Why?

Thank you very much.

3条回答
ら.Afraid
2楼-- · 2019-06-09 17:08

pass context of your activity in which you had set adapter

Context mContext;

//write this line in oncreate

   mContext=this; 
   RVAdapter adapter = new RVAdapter(array_restaurants,mContext);
   recList.setAdapter(adapter);

then in your adapter class use this context in place of v.getContext()

Context context;

RVAdapter(your array,Context mContext)
  {
    context=mContext;
  }

and use it like

personName.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {

            System.out.println("Context");
            System.out.println(context.toString());
            Intent intent = new Intent(context, Restaurante.class);
            context.startActivity(intent);
        }
    });

it may ask to cast context in startActivity with Acitivity you can do that.

查看更多
我只想做你的唯一
3楼-- · 2019-06-09 17:11

Pass the Activity object to your adapter not Context object. I saw your code. Replace this code at these lines MainActivity.java

Line 68:  RVAdapter adapter = new RVAdapter(restaurantes, MainActivity.this);

RVAdapter.java

Line 21: Activity mActivityContext; 

Line 55-58: RVAdapter(List<Restaurante> persons, Activity mActivityContext) {
        this.persons = persons;
        this.mActivityContext= mActivityContext;
    }

Hope this solves your problem.

查看更多
贼婆χ
4楼-- · 2019-06-09 17:20

After a lot of time I found an answer, the answer that those people submit help me to found the main problem, this was the solution

In the MainActivity I added a new parameter and pass the activity to the adapter, like @Meenal Sharma and @ch3tan proposed

RVAdapter adapter = new RVAdapter(restaurantes, this);

And in the adaptor

  public Adaptador(List<Restaurant> restaurants, Context context) {
    this.restaurants = restaurants;
    this.context = context;
}

And creating the intent:

Intent intent = new Intent(context, RestaurantActivity.class);
context.startActivity(intent);

That part solve an error but the main error that i had was, when I created a new activity with the Android Studio that created the new activity inheriting of ActionBarActivity, I changed ActionBarActivity for AppCompatActivity and everything worked again...

查看更多
登录 后发表回答