如何使用模型类从Firebase获取数据到列表

时间:2018-11-07 15:40:53

标签: java android firebase firebase-realtime-database

我在firebase中有一个这样的数据库 enter image description here

这是OrderListModel类

List<OrderListLocationModel> deliveryLocation = new ArrayList<>();
List<OrderListProductsModel> orderList = new ArrayList<>();
String orderStatus;
String orderTime;
String totalPrice;
String userName;

public OrderListModel(){

}

public OrderListModel(List<OrderListLocationModel> deliveryLocation, List<OrderListProductsModel> orderList, String orderStatus, String orderTime, String totalPrice, String userName) {
    this.deliveryLocation = deliveryLocation;
    this.orderList = orderList;
    this.orderStatus = orderStatus;
    this.orderTime = orderTime;
    this.totalPrice = totalPrice;
    this.userName = userName;
}
  //setter & getter

这是OrderListProductsModel

public OrderListProductsModel(){

}

public OrderListProductsModel(String productImage, String productName, String productPrice, String productQuantity, String productSize) {
    this.productImage = productImage;
    this.productName = productName;
    this.productPrice = productPrice;
    this.productQuantity = productQuantity;
    this.productSize = productSize;
}

 //setter & getter

这是OrderListLocationModel

public OrderListLocationModel(){

}

public OrderListLocationModel(String latitude, String longitude) {
    this.latitude = latitude;
    this.longitude = longitude;
}

//setter & getter

这是MainActivity代码

        private List<OrderListModel> orderList = new ArrayList<>();

        FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("productsOrderList");
    // Read from the database
    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
                OrderListModel orderListModel = (OrderListModel) postSnapshot.getValue(OrderListModel.class);
                //checking for order status
                String orderStatus =  postSnapshot.child("orderStatus").getValue().toString();
                //if order status in 0, then add to list
                if(orderStatus.equals("0")){
                    orderList.add(orderListModel);
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError error) {
            String e= error.getMessage();
            Log.d(TAG, "onCancelled: "+e);
        }
    });

我想通过模型类将Firebase中的所有数据获取到一个列表,并希望在回收器视图中显示该数据,但这使我感到错误

com.google.firebase.database.DatabaseException: Expected a List while deserializing, but got a class java.util.HashMap

0 个答案:

没有答案