Rahul Mishra
Rahul Mishra

Reputation: 593

Firebase JSON to Arraylist inside Arraylist with Objects Issues

This is the JSON object sample that I want to make class of:

{
  "products" : {
    "10001" : {
      "about" : "Marble Chowki pair with intricate",
      "category" : "handicrafts",
      "contact_vendor" : "09171430513",
      "images" : {
        "1001" : {
          "url" : "https://firebasestorage.googleapis.com/img1"
        },
        "1002" : {
          "url" : "https://firebasestorage.googleapis.com/img2"
        }
      },
      "shop_location" : "Stall No. 56",
      "vendor_address" : "Rajasthan Handicraft And Textiles"
}

and this is Class that I have created

class ProductEntity {
    int id;
    String about;
    String contact_vendor;
    String vendor_address;
    String category;
    List<ProductImage> image;
    public String shop_location;

    public ProductEntity(int id, String about, String contact_vendor,
                         String vendor_address, String category, ArrayList<ProductImage> image, String shop_location) {
        this.id = id;
        this.about = about;
        this.contact_vendor = contact_vendor;
        this.vendor_address = vendor_address;
        this.category = category;
        this.image = image;
        this.shop_location = shop_location;
    }

    public ProductEntity() {
    }
}

class ProductImage {
    String url ;

    public ProductImage() {
    }

    public ProductImage(String url) {
        this.url = url;
    }
}

I am not getting the image path with the data in i.e. ProductImage object inspite of having all the other values of ProductEntity object.

This is my Firebase instance that I am calling to populate data:

mDatabase = FirebaseDatabase.getInstance().getReference("products");
        mDatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {

                for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                    ProductEntity upload = postSnapshot.getValue(ProductEntity.class);
                    productList.add(upload);
                }

                productsRecyclerAdapter = new ProductsRecyclerAdapter(productList, CategoryActivity.this);
                products.setAdapter(productsRecyclerAdapter);
            }

Upvotes: 0

Views: 284

Answers (2)

Rahul Mishra
Rahul Mishra

Reputation: 593

This is the solution that worked for me:

for (DataSnapshot postSnapshot : snapshot.getChildren()) {
    ProductEntity upload = postSnapshot.getValue(ProductEntity.class);
    Log.d("error", "onDataChange: " + upload.about);

    List<ProductImage> images = new ArrayList<>() ;
    for(DataSnapshot datas : postSnapshot.child("images").getChildren()){

    ProductImage upload_image = datas.getValue(ProductImage.class);
    images.add(upload_image) ;
    }
    upload.image = images ;
    productList.add(upload);
}

Upvotes: 0

Peter Haddad
Peter Haddad

Reputation: 80914

ProductEntity upload;
mDatabase = FirebaseDatabase.getInstance().getReference("products");
    mDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {

            for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                upload = postSnapshot.getValue(ProductEntity.class);
            for(DataSnapshot datas : postSnapshot.getChildren()){
                  upload=datas.getValue(ProductEntity.class);
                productList.add(upload);
            }
            }
            productsRecyclerAdapter = new ProductsRecyclerAdapter(productList, CategoryActivity.this);
            products.setAdapter(productsRecyclerAdapter);
        }

Upvotes: 1

Related Questions