Kunal Shah
Kunal Shah

Reputation: 1

viewHolder.setItemClickListener() not working

ItemClickListener() interface

public interface ItemClickListener {

    void onClick(View view, int position, boolean isLongClick);

}

MyProductViewHolder class

public class MyProductViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener  {

    public TextView pname,pprice ;
    public ImageView pimage;
    ItemClickListener itemClickListener;

    public MyProductViewHolder(@NonNull View itemView) {
        super(itemView);
        pname = itemView.findViewById(R.id.product_name);
        pprice = itemView.findViewById(R.id.product_price);
        pimage = itemView.findViewById(R.id.product_image);
    }

    @Override
    public void onClick(View v) {

        itemClickListener.onClick(v,getAdapterPosition(),false);
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }
}

myproducts.class

private void loadProducts() {
        auth = FirebaseAuth.getInstance();
        user = auth.getCurrentUser();
        myProductref = FirebaseDatabase.getInstance().getReference("Admin").child(user.getUid()).child("Products");
        adapter = new FirebaseRecyclerAdapter<MyProducts, MyProductViewHolder>(
                MyProducts.class,
                R.layout.my_products,
                MyProductViewHolder.class,
                myProductref
        ) {
            @Override
            protected void populateViewHolder(final MyProductViewHolder viewHolder, final MyProducts model, int position) {

              Glide.with(getActivity()).load(model.getImage1()).into(viewHolder.pimage);
                viewHolder.pname.setText(model.getName());
                viewHolder.pprice.setText(model.getPrice());

                final String pid = model.getPid();
                final String name = model.getName();
                final String price = model.getPrice();
                final String description = model.getDescription();
                final String image1 = model.getImage1();
                final String image2 = model.getImage2();
                final String image3 = model.getImage3();
                final String uid = model.getUid();
                final String category = model.getCategoryName();
                final String date = model.getDate();
                final String time = model.getTime();
                final String location =model.getLocation();

                try {
                    viewHolder.setItemClickListener((view, position1, isLongClick) -> {

                        Intent intent = new Intent(getActivity(),ProductDetailsActivity.class);
                        intent.putExtra("pid",pid);
                        intent.putExtra("name",name);
                        intent.putExtra("price",price);
                        intent.putExtra("description",description);
                        intent.putExtra("image1",image1);
                        intent.putExtra("image2",image2);
                        intent.putExtra("image3",image3);
                        intent.putExtra("uid",uid);
                        intent.putExtra("category",category);
                        intent.putExtra("date",date);
                        intent.putExtra("time",time);
                        intent.putExtra("location",location);
                        startActivity(intent);
                    });
                }
                catch (Exception e){
                    FancyToast.makeText(getActivity(),e.getMessage(),FancyToast.LENGTH_LONG,FancyToast.ERROR,true).show();
                }


            }





        };
        adapter.notifyDataSetChanged();
        listproduct.setAdapter(adapter);
    }

The problem is in loadProducts() method Intent is not working in viewholder.setItemClickListener

Does somebody know why it doesn't work?

Upvotes: 0

Views: 71

Answers (1)

Max P
Max P

Reputation: 31

ViewHolder is not actually a view. No need to implement OnClickListener. Just add:

itemView.setOnClickListener((view) -> { itemClickListener.onClick(view, getAdapterPosition(), false) })

to the viewHolder constructor.

Upvotes: 1

Related Questions