My project is an e-commerce app. I want to display products under each category when clicked on the specific categories. I was able to display all products under a specific category but I am getting empty view holder layouts. I guess empty layouts are of other category products.
Here is the screenshot of homepage:
When I click on the circle images I am forwarded to respective category pages.
Here is the screenshot of the category men page where I am getting empty layouts:
Men.java
public class Men extends AppCompatActivity {
private DatabaseReference pdtref;
private RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
private String type=" ";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_men);
pdtref =FirebaseDatabase.getInstance().getReference().child("Products");
recyclerView = findViewById(R.id.recycle1);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
}
@Override
protected void onStart()
{
super.onStart();
FirebaseRecyclerOptions<Product> options =
new FirebaseRecyclerOptions.Builder<Product>().setQuery(pdtref, Product.class).build();
FirebaseRecyclerAdapter<Product, ProductViewHolder> adapter =
new FirebaseRecyclerAdapter<Product, ProductViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull final ProductViewHolder holder, final int
position, @NonNull final Product model) {
if (model.getCategory().equals("men")) {
holder.txtProductName.setText(model.getPname());
holder.txtProductDescription.setText(model.getDscp());
holder.txtProductPrice.setText("Price : ₹ " + model.getPrice());
Picasso.get().load(model.getImage()).into(holder.imageView);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Men.this, ProductDetails.class);
intent.putExtra("pid", model.getPid());
startActivity(intent);
}
});
}
}
@NonNull
@Override
public ProductViewHolder onCreateViewHolder (@NonNull final ViewGroup parent,
final int viewType){
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.productitems, parent, false);
ProductViewHolder holder = new ProductViewHolder(view);
return holder;
}
};
recyclerView.setAdapter(adapter);
adapter.startListening();
}
Men.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Men">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycle1"
/>
</RelativeLayout>
productitems.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_marginTop="20dp"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
app:cardElevation="15dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/product_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Product Name"
android:textAlignment="center"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="@color/black"
android:gravity="center_horizontal" />
<ImageView
android:id="@+id/product_image"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_below="@+id/product_name"
android:scaleType="centerCrop"
android:layout_marginTop="2dp"
/>
<TextView
android:id="@+id/product_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/product_image"
android:text="Product Price"
android:textSize="18dp"
android:textColor="@color/black"
android:gravity="center" />
<TextView
android:id="@+id/product_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/product_price"
android:text="Product Description"
android:layout_marginTop="2dp"
android:gravity="center"
android:textSize="16dp"
android:textColor="@color/black"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
Here is the screenshot of products details stored in firebase: