Reputation: 107
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
ProductFragment showing the data
public class ProductFragment extends Fragment {
private static final String TAG = "ProductFragment";
private FirebaseFirestore firestoreDB;
private RecyclerView productRecyclerView;
public ProductFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_product, container, false);
firestoreDB = FirebaseFirestore.getInstance();
productRecyclerView = (RecyclerView) view.findViewById(R.id.Product_RecyclerView);
LinearLayoutManager recyclerLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
productRecyclerView.setLayoutManager(recyclerLayoutManager);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(productRecyclerView.getContext(),
recyclerLayoutManager.getOrientation());
productRecyclerView.addItemDecoration(dividerItemDecoration);
getDocumentsFromCollection();
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
public void getDocumentsFromCollection() {
firestoreDB.collection("products").get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
//List<Event> eventList = new ArrayList<>();
List<DocumentSnapshot> documents = task.getResult().getDocuments();
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);
} 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
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ViewHolder>{
public static ClickListener clickListener;
public List<DocumentSnapshot> documents;
private Context context;
private FirebaseFirestore firestoreDB;
public ProductAdapter(List<DocumentSnapshot> list, Context 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 DocumentSnapshot snapshot = documents.get(position);
holder.item_name.setText(snapshot.getString("Product"));
holder.price.setText("Rs " + snapshot.getString("Cost") + "/" + snapshot.getString("Per"));
}
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);
}
}
I tried some methods but all of them crashed. I need to show a detailed view of the item clicked
Upvotes: 3
Views: 1637
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