Retrieving data using Map from Firebase Database

后端 未结 1 359
逝去的感伤
逝去的感伤 2020-12-11 08:32

I am trying to retrieve data from firebase database in Map format but I am getting a null pointer exception.I have successfully inserted data using map into my firebase data

相关标签:
1条回答
  • 2020-12-11 08:54

    Change your code at the time of retrieval

    Map<String, String> map  = (Map) dataSnapshot.getValue();
    

    Instead of

    Map map = (Map) dataSnapshot.getValue();
    

    Edit: You can retrieve list of recording using below code

    databaseReference1.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                Map<String, String> map = (Map) postSnapshot.getValue();
                if (map != null) {
                    String message = map.get("text");
                    String userName = map.get("Username");
                }
            }
        }
    
        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
    
    0 讨论(0)
提交回复
热议问题