Android-Expected a List while deserializing, but got a class java.util.HashMap

后端 未结 5 1273
小蘑菇
小蘑菇 2021-01-18 23:07

I want to get all list of restaurants from Firebase in android.

Here is my code:

boolean delivery;
String openTime, closeTime, restaurantName;
long          


        
5条回答
  •  庸人自扰
    2021-01-18 23:14

    I know this can be too late, but I wish my answer could help somebody. I know 2 ways to solve this problem. Once I have time I'll update my answer .

    #Solution 1

    First of all I copy & paste your code. It is not clear for me how did you add data to firebase, but I feel you added data directly to firebase.

    so this is My MainActivity code :

    public class MainActivity extends AppCompatActivity {
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            RestaurantModel rm;
            List utils;
    
            //restaurant 1
            rm = new RestaurantModel();
            rm.closeTime = "12:00am";
            rm.openTime = "12:00pm";
            rm.delivery = true;
            rm.likes = 300;
            rm.restaurantName = "KFC";
            utils = new ArrayList<>();
            utils.add("free wifi");
            utils.add("free Parking");
            utils.add("free water");
            rm.utilities = utils;
    
            //write first restaurant to Firebase
            rm.addRestaurantToFirebase(rm);
    
    
            //restaurant 2
            rm = new RestaurantModel();
            rm.closeTime = "5:00am";
            rm.openTime = "12:00pm";
            rm.delivery = false;
            rm.likes = 500;
            rm.restaurantName = "SubWay";
            utils = new ArrayList<>();
            utils.add("free wifi");
            utils.add("free Parking");
    
            rm.utilities = utils;
    
            //write Second restaurant to Firebase
            rm.addRestaurantToFirebase(rm);
    
    
            rm.getallRestaurant();
    
        }
    
    }
    

    and this is RestaurantModel :

    public class RestaurantModel {
        public boolean delivery;
        public String openTime;
        public String closeTime;
        public String restaurantName;
        public long likes;
        public List utilities;
    
    
        public List listRes;
    
        DatabaseReference dataResReference;
    
        public RestaurantModel() {
            dataResReference = FirebaseDatabase.getInstance().getReference().child("restaurants");
        }
    
        public List getallRestaurant() {
            listRes = new ArrayList<>();
    
            ValueEventListener valueEventListener = new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    for (DataSnapshot dataValues : dataSnapshot.getChildren()) {
                        RestaurantModel restaurantModel = dataValues.getValue(RestaurantModel.class);
                        listRes.add(restaurantModel);
    
                        Log.d("restaurantModel", restaurantModel.restaurantName);
                    }
                }
    
                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
    
                }
            };
            dataResReference.addListenerForSingleValueEvent(valueEventListener);
            return listRes;
        }
    
    
        public void addRestaurantToFirebase(RestaurantModel rm) {
            dataResReference.child(restaurantName).setValue(rm);
        }
    
    }
    

    here the result in firebase

提交回复
热议问题