0

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:

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:

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:

Here is the screenshot of products details stored in firebase

halfer
  • 19,824
  • 17
  • 99
  • 186
  • yes...empty item belongs to another category. just filter list as per need of categories. and load it. In onbindviewholder if condition doesn't match category thats what you got empty item in list – Manikandan Apr 13 '21 at 14:37
  • @Manikandan can you tell me how to filter the list – Project Android Apr 13 '21 at 14:53
  • Make sure all items have men category otherwise filter the list with men category otherwise remove if (model.getCategory().equals("men")) condition in onbindviewholder – Manikandan Apr 13 '21 at 14:57
  • var month: List = arrayListOf("January", "February", "March") // to get the result as list var monthList: List = month.filter { s -> s == "January" } – Manikandan Apr 13 '21 at 15:01
  • @Manikandan I want to get category from the firebase – Project Android Apr 13 '21 at 15:16
  • did you get empty item after removed if condition (model.getCategory().equals("men")) in onbindviewholder. – Manikandan Apr 13 '21 at 15:31
  • @Manikandan No I didn't get empty item after removing if condition...but all the products under other categories are been displayed. I want display products under men category in Men page, products under women category in women page.... – Project Android Apr 13 '21 at 15:47
  • Im not aware of Firebase list..But sure there is option query for filter once you get filtered data you don't want use condition in onbindviewmodel. refer ->https://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase ..all d best – Manikandan Apr 13 '21 at 16:09
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Apr 13 '21 at 21:19

0 Answers0