Java Firebase JSON到Arraylist内部的Arraylist,存在对象问题 这是我想要创建的JSON对象示例: 这是我创建的类
这是对我有效的解决方案:Java Firebase JSON到Arraylist内部的Arraylist,存在对象问题 这是我想要创建的JSON对象示例: 这是我创建的类,java,android,json,firebase,firebase-realtime-database,Java,Android,Json,Firebase,Firebase Realtime Database,这是对我有效的解决方案: for (DataSnapshot postSnapshot : snapshot.getChildren()) { ProductEntity upload = postSnapshot.getValue(ProductEntity.class); Log.d("error", "onDataChange: " + upload.about); List<ProductImage> images = new ArrayList&l
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);
}
(DataSnapshot postSnapshot:snapshot.getChildren())的{
ProductEntity upload=postSnapshot.getValue(ProductEntity.class);
Log.d(“错误”,“onDataChange:+upload.about”);
列表图像=新的ArrayList();
对于(DataSnapshot数据:postSnapshot.child(“图像”).getChildren()){
ProductImage upload\u image=datas.getValue(ProductImage.class);
添加(上传图片);
}
upload.image=图像;
productList.add(上传);
}
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;
}
}
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);
}
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);
}