I am creating an Android Grocery Shop App, where firestore used. I am implementing a Nested Recyclerview where I have done coding for showing data from firestore to recycler view by Category Names.
But, all products are showing in the recyclerview, like the below image. Data is not sorted by category names.
Codes
ProductsAdapterbyCategory.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.chattthedev.shoppingguskara.Activities.Models.ProductsModel;
import com.chattthedev.shoppingguskara.R;
import java.util.List;
public class ProductsAdapterbyCategory extends
RecyclerView.Adapter<ProductsAdapterbyCategory.ViewHolderForProducts> {
List<ProductsModel> productsbycat;
Context context;
public ProductsAdapterbyCategory(List<ProductsModel> productsbycat, Context context) {
this.productsbycat = productsbycat;
this.context = context;
}
@NonNull
@Override
public ViewHolderForProducts onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.singleforitembycat, parent, false);
ViewHolderForProducts viewHolderForProducts = new ViewHolderForProducts(view);
return viewHolderForProducts;
}
@Override
public void onBindViewHolder(@NonNull ViewHolderForProducts holder, int position) {
holder.catnameinprod.setText(productsbycat.get(position).getItemCategory());
ProductsAdapter2 productsAdapter2 = new ProductsAdapter2(productsbycat, context);
holder.recyclerViewinprod.setHasFixedSize(true);
holder.recyclerViewinprod.setLayoutManager(new LinearLayoutManager(context, RecyclerView.HORIZONTAL, false));
holder.recyclerViewinprod.setAdapter(productsAdapter2);
holder.recyclerViewinprod.setNestedScrollingEnabled(false);
}
@Override
public int getItemCount() {
return (productsbycat != null ? productsbycat.size() : 0);
}
public class ViewHolderForProducts extends RecyclerView.ViewHolder {
public TextView catnameinprod;
public Button viewmoreinprod;
public RecyclerView recyclerViewinprod;
public ViewHolderForProducts(@NonNull View itemView) {
super(itemView);
catnameinprod = itemView.findViewById(R.id.catnameforfetch);
viewmoreinprod = itemView.findViewById(R.id.buttviewmoreitem);
recyclerViewinprod = itemView.findViewById(R.id.recyclerprodfetch);
}
}
}
ProductsAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Filter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.chattthedev.shoppingguskara.Activities.Models.ProductsModel;
import com.chattthedev.shoppingguskara.R;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.List;
public class ProductsAdapter2 extends RecyclerView.Adapter {
List<ProductsModel> productsModelList;
Context context;
public ProductsAdapter2(List<ProductsModel> productsModelList, Context context) {
this.productsModelList = productsModelList;
this.context = context;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.horizontal_scroll_item, parent, false);
ProductsAdapter2.ViewHolderClass2 viewHolderClass = new ProductsAdapter2.ViewHolderClass2(view);
return viewHolderClass;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
ProductsAdapter2.ViewHolderClass2 viewHolderClass = (ProductsAdapter2.ViewHolderClass2) holder;
ProductsModel productsModel = productsModelList.get(position);
viewHolderClass.proditemname.setText(productsModel.getItemname());
viewHolderClass.proditemprice.setText("Rs." + productsModel.getItemActualPrice() + "/-");
Picasso.get().load(productsModel.getItemImageUrl()).into(viewHolderClass.prodimage);
}
public void updatelist(List<ProductsModel> models){
productsModelList = new ArrayList<>();
productsModelList.addAll(models);
notifyDataSetChanged();
}
public Filter getFilter(){
return new Filter() {
private List<ProductsModel> filtered = new ArrayList<ProductsModel>();
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString().toLowerCase();
filtered.clear();
if(charString.isEmpty()){
filtered = productsModelList;
//filteredCUG = CUG;
}
else{
for (ProductsModel cug : productsModelList){
if( cug.getItemname().toLowerCase().contains(charString)){
filtered.add(cug);
}
}
//filteredCUG = filtered;
}
FilterResults filterResults = new FilterResults();
filterResults.values = filtered;
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
productsModelList = (List<ProductsModel>) filterResults.values;
notifyDataSetChanged();
}
};
}
@Override
public int getItemCount() {
return productsModelList.size();
}
public class ViewHolderClass2 extends RecyclerView.ViewHolder {
TextView proditemname, proditemprice, productitemdiscount;
ImageView prodimage;
public ViewHolderClass2(@NonNull View itemView) {
super(itemView);
proditemname = itemView.findViewById(R.id.hsproducttitle);
proditemprice = itemView.findViewById(R.id.hsproductprice);
prodimage = itemView.findViewById(R.id.hsproductimage);
productitemdiscount = itemView.findViewById(R.id.hsproductdesc);
}
}
}
HomeFragment.java only providing the recyclerview fetching data function from firestore
private void getproductsfromcats() {
Query query = collectionReference.orderBy("ItemCategory");
query.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for (QueryDocumentSnapshot queryDocumentSnapshot : queryDocumentSnapshots) {
ProductsModel productsModel = queryDocumentSnapshot.toObject(ProductsModel.class);
productsModelList2.add(productsModel);
}
productsAdapterbyCategory = new
ProductsAdapterbyCategory(productsModelList2,view.getContext());
product_items_for_cat.setAdapter(productsAdapterbyCategory);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(view.getContext(), "Failed", Toast.LENGTH_SHORT).show();
}
});
}
ProductsModel.java
public class ProductsModel {
private String ItemActualPrice, ItemCategory, ItemDescription, ItemDiscount,
ItemFinalPrice, ItemId, ItemImageUrl, ItemStock, ItemUnit, ItemWeight,
Itemname;
public ProductsModel() {
}
public ProductsModel(String itemActualPrice, String itemCategory, String
itemDescription, String itemDiscount, String itemFinalPrice, String itemId,
String itemImageUrl, String itemStock, String itemUnit, String itemWeight,
String itemname) {
ItemActualPrice = itemActualPrice;
ItemCategory = itemCategory;
ItemDescription = itemDescription;
ItemDiscount = itemDiscount;
ItemFinalPrice = itemFinalPrice;
ItemId = itemId;
ItemImageUrl = itemImageUrl;
ItemStock = itemStock;
ItemUnit = itemUnit;
ItemWeight = itemWeight;
Itemname = itemname;
}
public String getItemActualPrice() {
return ItemActualPrice;
}
public void setItemActualPrice(String itemActualPrice) {
ItemActualPrice = itemActualPrice;
}
public String getItemCategory() {
return ItemCategory;
}
public void setItemCategory(String itemCategory) {
ItemCategory = itemCategory;
}
public String getItemDescription() {
return ItemDescription;
}
public void setItemDescription(String itemDescription) {
ItemDescription = itemDescription;
}
public String getItemDiscount() {
return ItemDiscount;
}
public void setItemDiscount(String itemDiscount) {
ItemDiscount = itemDiscount;
}
public String getItemFinalPrice() {
return ItemFinalPrice;
}
public void setItemFinalPrice(String itemFinalPrice) {
ItemFinalPrice = itemFinalPrice;
}
public String getItemId() {
return ItemId;
}
public void setItemId(String itemId) {
ItemId = itemId;
}
public String getItemImageUrl() {
return ItemImageUrl;
}
public void setItemImageUrl(String itemImageUrl) {
ItemImageUrl = itemImageUrl;
}
public String getItemStock() {
return ItemStock;
}
public void setItemStock(String itemStock) {
ItemStock = itemStock;
}
public String getItemUnit() {
return ItemUnit;
}
public void setItemUnit(String itemUnit) {
ItemUnit = itemUnit;
}
public String getItemWeight() {
return ItemWeight;
}
public void setItemWeight(String itemWeight) {
ItemWeight = itemWeight;
}
public String getItemname() {
return Itemname;
}
public void setItemname(String itemname) {
Itemname = itemname;
}
}
Firestore DB Structure Screenshot
you can create two different collection(meat,fruit) in firestore