从RecycleView适配器启动活动上点击 [英] Start Activity from RecycleView Adapter on click
问题描述
我已经开始从CardView适配器一个新的活动的问题,这是code:
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);
和之后在适配器。我设置一个OnClickListener
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);
}
});
当我打印的背景下,在控制台一切看起来正常,但aplication后停止工作。为什么呢?
When i print the context in the console everything looks fine, but after the aplication stop working. Why?
非常感谢你。
推荐答案
在很多时候,我找到了答案,答案是那些人提出帮我发现的主要问题,这是解决方案
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
在MainActivity我增加了一个新的参数和活动传递给适配器,如@Meenal Sharma和建议@ ch3tan
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);
和在适配器
public Adaptador(List<Restaurant> restaurants, Context context) {
this.restaurants = restaurants;
this.context = context;
}
和创造的意图:
Intent intent = new Intent(context, RestaurantActivity.class);
context.startActivity(intent);
这部分解决了一个错误,但是,我不得不是,当我创建了Android Studio中创建新活动继承ActionBarActivity的新活动的主要错误,我改变ActionBarActivity的AppCompatActivity,一切又激动...
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...
这篇关于从RecycleView适配器启动活动上点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!