问题描述
我已经使用网格布局管理器实现了回收站视图以创建类别列表.现在,基本上我已经尝试在我的OnCreateViewholder方法内的持有人中实现setOnClick侦听器.在运行代码时,出现此错误
I have implemented a recycler view with grid layout manager to create a category list. Now basically i have tried to implement a setOnClick Listener in my holder inside OnCreateViewholder Method. While running the code, I am getting this error
android.util.AndroidRuntimeException:从Activity上下文外部调用startActivity()需要FLAG_ACTIVITY_NEW_TASK标志.这真的是您想要的吗?
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
这是我的适配器代码
package com.paaltao.Adapters;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.paaltao.R;
import com.paaltao.activity.ProductDetailsActivity;
import com.paaltao.classes.Category;
import java.util.Collections;
import java.util.List;
/**
* Created by Arindam on 02-Feb-15.
*/
public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.CategoryHolder> {
private Context context;
private LayoutInflater inflater;
private View view;
List<Category> data = Collections.emptyList();
public CategoryAdapter(Context context ,List<Category> data){
this.data = data;
this.context= context;
}
@Override
public CategoryHolder onCreateViewHolder(ViewGroup parent, int viewType) {
inflater = LayoutInflater.from(parent.getContext());
view = inflater.inflate(R.layout.category_card, parent, false);
CategoryHolder holder = new CategoryHolder(view);
holder.categoryImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context,ProductDetailsActivity.class);
context.startActivity(intent);
}
});
return holder;
}
@Override
public void onBindViewHolder(CategoryHolder holder, int position) {
Category current = data.get(position);
holder.categoryName.setText(current.getCategory_name());
holder.categoryImage.setImageResource(current.getCategory_id());
}
@Override
public int getItemCount() {
return data.size();
}
class CategoryHolder extends RecyclerView.ViewHolder {
TextView categoryName;
ImageView categoryImage;
public CategoryHolder(View itemView) {
super(itemView);
categoryImage = (ImageView) itemView.findViewById(R.id.category_image);
categoryName = (TextView) itemView.findViewById(R.id.category_name);
}
}
}
推荐答案
private Context context;
中的上下文很可能不是活动上下文.初始化活动时,您需要将其活动作为CategoryAdapter的第一个参数传递:CategoryAdapter adapter = new CategoryAdapter(activity, data);
The context in private Context context;
is most likely not an activity context. You need to pass your activity as the first parameter of CategoryAdapter when you initialize it: CategoryAdapter adapter = new CategoryAdapter(activity, data);
这篇关于无法从Recycler View适配器的setOnClick侦听器中启动活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!