2016年8月11日 星期四

if we use push method to save the object,has a unique id,how to get the useful data?

31 if we use push method to save the object,has a unique id,how to get the useful data?

like tha,how can i get the Chat?




DatabaseReference ref = FirebaseDatabase.getInstance().getReference();

ref.limitToLast(5).addValueEventListener(new ValueEventListener() {
   @Override
   public void onDataChange(DataSnapshot snapshot) {
       for (DataSnapshot msgSnapshot: snapshot.getChildren()) {
           Chat msg = msgSnapshot.getValue(Chat.class);
           Log.i("Chat", chat.getName()+": "+chat.getText());
       }
   }
   @Override
   public void onCancelled(DatabaseError firebaseError) {
       Log.e("Chat", "The read failed: " + firebaseError.getText());
   }
});


can read all the Chat