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