firebase DatabaseException:反序列化时需要一个List,但是得到了一个类java.util.HashMap

时间:2018-08-01 19:43:04

标签: java android firebase firebase-realtime-database pojo

我已经全局初始化了arraylist(mDataset)。我试图根据某些条件填充arraylist。 发生错误的代码段。 当我尝试使用for循环中从fire.getValue()接收的值初始化对象产品时发生错误

mProductReference.child("Electronics").addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        for(DataSnapshot fire : dataSnapshot.getChildren())
                        {
                            Product product = fire.getValue(Product.class);
                            if(product.getId().equals(key)) {
                                mDataSet.add(product);
                            }
                            //Toast.makeText(MyProduct.this,mDataSet.size()+ "", Toast.LENGTH_SHORT).show();
                        }
                        mAdapter.refresh(mDataSet);
                        mRecyclerView.setAdapter(mAdapter);
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });

POJO(Product.java)

public class Product {
String name,desc,image,id,userid,category;
long price,quantity,noOfRating;
long rating;
ArrayList<Review> review;
public Product() {
}
public Product(String name, String desc, String image, String id, String userid, String category, long price, long quantity, long noOfRating, long rating, ArrayList<Review> review) {
    this.name = name;
    this.desc = desc;
    this.image = image;
    this.id = id;
    this.userid = userid;
    this.category = category;
    this.price = price;
    this.quantity = quantity;
    this.noOfRating = noOfRating;
    this.rating = rating;
    this.review = review;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getDesc() {
    return desc;
}

public void setDesc(String desc) {
    this.desc = desc;
}

public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getUserid() {
    return userid;
}

public void setUserid(String userid) {
    this.userid = userid;
}

public String getCategory() {
    return category;
}

public void setCategory(String category) {
    this.category = category;
}

public long getPrice() {
    return price;
}

public void setPrice(long price) {
    this.price = price;
}

public long getQuantity() {
    return quantity;
}

public void setQuantity(long quantity) {
    this.quantity = quantity;
}

public long getNoOfRating() {
    return noOfRating;
}

public void setNoOfRating(long noOfRating) {
    this.noOfRating = noOfRating;
}

public long getRating() {
    return rating;
}

public void setRating(long rating) {
    this.rating = rating;
}

public ArrayList<Review> getReview() {
    return review;
}

public void setReview(ArrayList<Review> review) {
    this.review = review;
}}

它正在从事其他活动,但在此活动中引起了问题。

0 个答案:

没有答案