Ваш запрос из Firebase выполняется асинхронно, поэтому данные запроса возвращаются и добавляются в список продуктов после того, как вы создали экземпляр адаптера.
Ваш оператор if не выполняется, потому что список продуктов фактически пуст, потому что этот код выполняется до того, как запрос вернется.
// This is false here because this block is getting executed before
// the Firebase query returns the data. However your not seeing the
// toast, because you are passing the wrong context. Toast takes
// an activity context, not application context.
if(prod.size()>0) {
adapter = new ProductAdapter(prod);
RecyclerView recview = findViewById(R.id.items_recycler_view);
recview.setHasFixedSize(true);
recview.setLayoutManager(new LinearLayoutManager(this));
recview.setAdapter(adapter);
}else{
Toast.makeText(getApplicationContext(),"this list is empty",Toast.LENGTH_LONG).show();
}
Попробуйте немного изменить свой код, чтобы создать адаптер, затем добавьте данные, как только он вернется из запроса:
adapter = new ProductAdapter();
RecyclerView recview = findViewById(R.id.items_recycler_view);
recview.setHasFixedSize(true);
recview.setLayoutManager(new LinearLayoutManager(this));
recview.setAdapter(adapter);
products.whereEqualTo("IDliv","kITQ8wiPshsnWqHDlP5D").addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot documentSnapshots, @Nullable FirebaseFirestoreException e) {
assert documentSnapshots != null;
for (DocumentChange document : documentSnapshots.getDocumentChanges()) {
generalObject obj=document.getDocument().toObject(generalObject.class);
for(int i=0;i<obj.getproducts().size();i++){
String[] body;
body=obj.getproducts().get(i).toString().split("\\+");
ProductModel p = new ProductModel(body[0].toString(), body[1].toString(), body[2].toString());
prodList.add(p);
}
// Tell the adapter we have new data that we want to add
adapter.addNewData(prodList)
}
}
});
Затем немного измените свой адаптер:
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ViewHolder> {
private ArrayList<ProductModel> products;
public ProductAdapter() {
this.products = new ArrayList<ProductModel>();
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = (View) LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_product, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
ProductModel prod = products.get(position);
holder.name.setText(prod.getGetProductName());
holder.description.setText(prod.getProductMarque());
holder.qty.setText(prod.getProductQte());
}
@Override
public int getItemCount() {
if (products != null) {
return products.size();
} else {
return 0;
}
}
public void addNewData(data: List<ProductModel>) {
this.products.clear();
this.products.addAll(data);
notifyDatasetChanged(); //This will tell the adapter we have new data and to re-inflate the views
}
}