0

i'm downloading Data from my Firebase Firestore DB. To add every Item in my ArrayList i created the following two methods:

   private void getProductDetails(ArrayList<HashMap<String, Object>> product_seller, ArrayList<SellerObject> seller) {
    rvValues = new ArrayList<>();

    for (int i = 0; i < seller.size(); i++) {
        SellerObject so = seller.get(i);
        Log.i(TAG, "getProductDetails: sotze " + seller.size());


        rvValues.add(getProductData(so));

        progressBar_products.setVisibility(View.INVISIBLE);


    }

        pa = new productsAdapter(getActivity(), rvValues);
        rv.setAdapter(pa);


}


private ProductObject getProductData(SellerObject so) {
    for (int in = 0; in < so.getProductValues().size(); in++) {

        HashMap<String, Object> product_values = so.getProductValues().get(in);


        final DocumentReference docRef = db.collection("Products").document(String.valueOf(product_values.get("productId")));

        docRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
            @Override
            public void onSuccess(DocumentSnapshot documentSnapshot) {
                po = documentSnapshot.toObject(ProductObject.class);
                po.setSo(so);
                po.setPrice(String.valueOf(product_values.get("price")));
                po.setSize(String.valueOf(product_values.get("size")));


            }
        });

       return po;
    }
    return po;
}

The problem now is that i'm returning a ProductObject which is null. I'm declaring the PO at the the top of my class:

    private ProductObject po = new ProductObject();

So how can i return the value after finishing the for loop?

Dominik
  • 419
  • 2
  • 6
  • 16
  • You are getting null because your `for loop` is synchronous which loop to end before asynchronous addOnSuccessListener's `onSuccess` method is called – Ali Ahsan Jan 12 '19 at 15:43
  • Please check the duplicate to see why do you have this behaviour and how can you solve this using a custom callback. – Alex Mamo Jan 13 '19 at 08:59

1 Answers1

0

Your pojo class variables must have same name as defined in the Firestone.

Please check if every variable of your pojo class i.e. (ProductObject) matches the same properties under collection "Products"in Firestone

Rakesh Verma
  • 766
  • 6
  • 14
  • i receive the values in the methods but if i return it its null. if i print it out in the listeners i receive all values – Dominik Jan 12 '19 at 15:31
  • Are you returning the same instance of ProjectObject to which you are assigning the received data from Firestone ? As in the question I cannot see the Object declaration of ProjectObject private ProductObject getProductData(SellerObject so) { // You should add it here ProjectObject po=null; for (int in = 0; in < so.getProductValues().size(); in++) { – Rakesh Verma Jan 12 '19 at 15:33
  • no i return it after the for loop – Dominik Jan 12 '19 at 15:42
  • If you are getting the response in onSuccess function , there is some issue while writing response to your pojo class. Please check if every variable of your pojo class i.e. (ProductObject) matches the same properties under collection "Products"in Firestone – Rakesh Verma Jan 13 '19 at 08:19