Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – viewHolder.setItemClickListener() not working

Posted on November 11, 2022 By

Solution 1 :

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.

Problem :

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?

Comments

Comment posted by Max P

Anywhere in the viewHolder constructor.

READ  [FIXED] android - does edittext retain the data when configuration change ? do I need view model if I only have edittext in my screen?
Powered by Inline Related Posts
Android Tags:android, firebase, java

Post navigation

Previous Post: [FIXED] android – Convert BMP image to PNG or JPG in Java without loading the entire image in memory
Next Post: [FIXED] xml – Android – prevent TextView to push out another element inside LinearLayout

Related Posts

[FIXED] Probelm with Server-Client Application using Usb with Adb (Android & Node.js) Android
[FIXED] java – Change RecyclerView Contents After Clicking On an Item in a DropDown Menu Android
[FIXED] android – VPN Service setHttpProxy with Pac URI Android
[FIXED] android – Xamarin – Permission “REQUEST_INSTALL_PACKAGES” – Which dependency brings this permission Android
[FIXED] java – Keeping saved files of app after an update Android
[FIXED] android – How to control left and right volume of audio separately(imagine wearing a headphone) while playing a sound in flutter app? Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can VPN be traced by police?
  • Where were Kaiser-Frazer cars built?
  • How do you make gold rose gold paint?
  • What are the newest type of dentures?
  • Can you wear joggers as dress pants?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme