sibin
sibin

Reputation: 107

How to pass document id of Firestore document through OnclickListener?

I want to add onClickListener to items in the recyclerview showing data from firestore. OnClick should pass the corresponding document id through intent . Pleas help

I tried some methods but all of them crashed. I need to show a detailed view of the item clicked

Upvotes: 3

Views: 1637

Answers (1)

Unofficialpage
Unofficialpage

Reputation: 67

You can something like this: Create POJO class and get data from this class.

POJO.class

public class Info {
String product;
String cost;
String per;
@Exclude
private String key;

public Info(){
}

public Info(String product, String cost, String per){
this.product = product;
this.cost = cost;
this.per = per;
}

public <T extends Info> T withId(@NonNull final String id) {
    this.key = id;
    return (T) this;
}

public String getProduct() {
    return product;
}

public void setProduct(String product) {
    this.product = product;
}

public String getCost() {
    return cost;
}
public void setCost(String cost) {
    this.cost = cost;
}

public String getPer() {
    return per;
}


public void setPer(String per) {
    this.per = per;
}
}

Your fragment, your method

  List<Info> documents = new ArrayList<>(); //before onCreate method
  public void getDocumentsFromCollection() {
  firestoreDB.collection("products").get()
        .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
      for (DocumentSnapshot snap : task.getResult()){
                   Info model =snap.toObject(Info.class).withID(snap.getID());

   documents.add(model);


                    ProductAdapter recyclerViewAdapter = new     ProductAdapter(documents, getActivity(), firestoreDB);



                    recyclerViewAdapter.setOnItemClickListener(new ProductAdapter.ClickListener() {
                        @Override
                        public void onItemClick(int position, View v) {
                            Log.d(TAG, "onItemClick position: " + position);

                                // Go to the details page for the selected       product



                            }

                    });

                    productRecyclerView.setAdapter(recyclerViewAdapter);
                    productRecyclerView.notifyDataSetChanged();
       }

                } else {
                    Log.d(TAG, "Error getting documents: ", task.getException());
                }
            }
        });

firestoreDB.collection("products")
        .addSnapshotListener(getActivity(), new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {


            }
        });
  }

Adapter your:

public class ProductAdapter  extends  RecyclerView.Adapter<ProductAdapter.ViewHolder>{

public static ClickListener clickListener;

public List<Info> documents;
private Activity context;
private FirebaseFirestore firestoreDB;

public ProductAdapter(List<Info> list, Activitx ctx,   FirebaseFirestore firestore) {
documents = list;
context = ctx;
firestoreDB = firestore;
}

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

@Override
public ProductAdapter.ViewHolder
 onCreateViewHolder(ViewGroup parent, int viewType) {

 View view = LayoutInflater.from(parent.getContext())
        .inflate(R.layout.list_layout, parent, false);

 ProductAdapter.ViewHolder viewHolder =
        new ProductAdapter.ViewHolder(view);
 return viewHolder;
}

@Override
 public void onBindViewHolder(ProductAdapter.ViewHolder holder, int position) {
 final int itemPos = position;
 final Info model = documents.get(position);
 holder.item_name.setText(model.getProduct());
  holder.price.setText("Rs " + model.getCost() + "/" +  model.getPer());

  holder.itemView.setOnClicListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               Intent openNewActivity = new Intent(context, YourNewActivity.class);
              openNewActivity.putExtra("id", model.getId());
              openNewActivity.putExtra("product", model.getProduct());
              context.startActivity(openNewActivity);
            }
        }); 


 }

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

  public TextView item_name;
  public TextView price;


     public ViewHolder(View view) {
     super(view);
     itemView.setOnClickListener(this);
     item_name = view.findViewById(R.id.List_maintext);
     price = view.findViewById(R.id.List_subtext);

  }

 @Override
  public void onClick(View v) {
    clickListener.onItemClick(getAdapterPosition(), v);

  }
  } 
  public void setOnItemClickListener(ClickListener clickListener) {
  ProductAdapter.clickListener = clickListener;


}

public interface ClickListener {
void onItemClick(int position, View v);

}

}

You can try something like this, I think this can help you to solve your problem. P.S. Sorry for my english.

Upvotes: 2

Related Questions