How to Convert Firebase data to Java Object…?

后端 未结 5 1041
独厮守ぢ
独厮守ぢ 2020-11-27 12:12

Using Firebase Library to send data to the server in the form Message(String, String) added to the HashMap

Exampl

5条回答
  •  春和景丽
    2020-11-27 12:22

    You could make it using a ChildEventListener instead of a ValueEventListener.

    For example. I have a class called match with 3 string properties {id, name, photoUrl} then I add all the values in a List. Instead of using a for loop it works for me with this code.

        Query queryRef = mDatabase.child("users").child(user.getUid()).child("matches");
    
        ChildEventListener listener = new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
                Match match = dataSnapshot.getValue(Match.class);
                matchList.add(match);
                mAdapter = new MatchAdapter(getContext(), matchList);
                recyclerView.setAdapter(mAdapter);
            }
            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
                Log.d("MESSAGEFRAGMENT", "CHILDCHANGE");
            }
            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {
            }
    
            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {
    
            }
    
            @Override
            public void onCancelled(DatabaseError databaseError) {
    
            }
        };
        queryRef.addChildEventListener(listener);
    

提交回复
热议问题