java - Firebase JSON 到 Arraylist 内的 Arraylist 存在对象问题

标签 java android json firebase firebase-realtime-database

这是我想要创建类的 JSON 对象示例:

{
  "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"
}

这是我创建的类<​​/h2>
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;
    }
}

尽管具有 ProductEntity 对象的所有其他值,但我没有获取包含 ProductImage 对象中的数据的图像路径。

这是我调用它来填充数据的 Firebase 实例:

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);
            }

最佳答案

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);
        }

关于java - Firebase JSON 到 Arraylist 内的 Arraylist 存在对象问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47230228/

相关文章:

java - 使用代理的性能影响

java - 对象何时有资格进行垃圾回收

java - 如何在 Android 中写入 SD 卡上的文件夹?

javascript - 我无法对来自 redis 的 JSON 数据使用 parseInt ?

java - Volley : Create a method that takes care of both JSONArrayRequest and JSONObjectRequest

java - 如何从 javadoc Ant 任务的结果中排除特定方法/构造函数?

android - 无法解析拆分 Gradle 构建脚本中的库类

android - 向 Google Play Android Developer API 发出 HTTP post 请求

javascript - 如何向 GLTF 模型添加光泽度/镜面纹理?

json - 同一安全组的 CidrIp json 模板