recyclerview适配器上的java上下文

cdmah0mi  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(311)

我试图将上下文从java类发送到 RecyclerView ,这样我就可以单击元素并重定向到意图。显然,上下文不对。我已经评论了它的台词,应用程序也可以运行,但只要我试图证明它,它就会再次崩溃。我假设这是一个上下文的东西,但是如果有另一种方法来为我的元素设置click方法的话,它将非常有用。
它引发的异常为空指针异常:
java.lang.runtimeexception:无法启动活动:java.lang.nullpointerexception:尝试对空对象引用调用虚拟方法“void android.widget.button.setonclicklistener(android.view.view$onclicklistener)”
以下是适配器代码:

public class SubsidioAdapter extends RecyclerView.Adapter<SubsidioAdapter.SubsidioViewHolder> {

    private Context context;
    private List<Producto> productoList;
    private LayoutInflater inflater;

    public SubsidioAdapter(Context context, List<Producto> productoList) {
        this.context = context;
        this.productoList = productoList;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public SubsidioViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = this.inflater.inflate(R.layout.producto_item, null);
        return new SubsidioViewHolder(view);
    }

    @Override
    public void onBindViewHolder(SubsidioViewHolder subsidioViewHolder, int position) {
        subsidioViewHolder.imageView.setImageResource(this.productoList.get(position).getFoto());
        subsidioViewHolder.textView1.setText(this.productoList.get(position).getNombre());
        subsidioViewHolder.textView2.setText(this.productoList.get(position).getDescripcion());
        subsidioViewHolder.textView3.setText(this.productoList.get(position).getPrecio());
        subsidioViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                context.startActivity(new
                        Intent(context, ModificarProductoActivity.class));
            }
        });
    }

    @Override
    public int getItemCount() {
        return this.productoList.size();
    }

    public class SubsidioViewHolder extends RecyclerView.ViewHolder {

        ImageView imageView;
        TextView textView1;
        TextView textView2;
        TextView textView3;

        public SubsidioViewHolder(View itemView) {
            super(itemView);

            this.imageView = itemView.findViewById(R.id.fotoSub);
            this.textView1 = itemView.findViewById(R.id.descSub);
            this.textView2 = itemView.findViewById(R.id.nombreSub);
            this.textView3 = itemView.findViewById(R.id.precioSub);

        }
    }
}
qcbq4gxm

qcbq4gxm1#

将id设置为适配器项布局的根元素,然后按id查找该视图并使用它而不是 itemView ,而不是:

subsidioViewHolder.itemView.setOnClickListener(new View.OnClickListener(){
        public void onClick(View view) {
          ...
        }
    });

用途:

subsidioViewHolder.rootLayout.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view) {
              ...
            }
        });

. 请记住,可以在oncreateviewholder()内的recyclerview适配器中获取上下文,而无需使用以下构造函数:

@Override
public SubsidioViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    context=viewGroup.getContext();
    View view = this.inflater.inflate(R.layout.producto_item, null);
    return new SubsidioViewHolder(view);
}

虽然看起来你的问题是

相关问题