当前位置: 动力学知识库 > 问答 > 编程问答 >

android - How Can I get ArrayList of Java Objects of all Childs from Firebase

问题描述:

How Can I get ArrayList of Java Objects of all Childs from Firebase using updated firebase commands, currently I am using below approach but not able to get the list.

ValueEventListener postListener = new ValueEventListener() {

@Override

public void onDataChange(DataSnapshot dataSnapshot) {

// Get Post object and use the values to update the UI

if(dataSnapshot!=null){

for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {

UserDataClass post = postSnapshot.getValue(UserDataClass.class);

members.add(post);

}

}

}

@Override

public void onCancelled(DatabaseError databaseError) {

// Getting Post failed, log a message

}

};

mDatabaseRef.addValueEventListener(postListener);

below is datasnapshot which is returning from firebase but I am not able to get the desired result out of it.

DataSnapshot { key = 12345, value = {Members={00000000={phoneNo=00000000, longitude=73.0703307, latitude=33.6396975, password=qwertyuiop, CName=00000000, admin=true, name=Anwar Kamal}, 03028084374={phoneNo=03028084374, longitude=73.0701292, latitude=33.6397129, password=qwerty, CName=00000000, admin=false, name=Nehal Khan}, 03028084516={phoneNo=03028084516, longitude=73.0702659, latitude=33.6397622, password=qwerty, CName=03035356317, admin=false, name=Jamal Khan}}} }

all i want is list of all members

and my java object is

public class UserDataClass {

public double latitude;

public double longitude;

public String Name="";

public String password="";

public String phoneNo="";

public String CircleName="";

public boolean isAdmin=false;

}

网友答案:

The ValueEventListener has another purpose. You should use ChildEventListener and also dataSnapshop.getValue() to implement what you want, for example:

ChildEventListener childEventListener = new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {

        UserDataClass post =    postSnapshot.getValue(UserDataClass.class);
        members.add(post);
    }

  //...

Note: You should use the listener with the current firebase_referance to the Users table. Hence you should replace

    mDatabaseRef.addValueEventListener(postListener);

with

    mDatabaseRef.child("Members").addChildEventListener(childEventListener);

notice that your full path of the Members table is colored and i couldn't figure the path.

You can find the whole documentation here

good luck

分享给朋友:
您可能感兴趣的文章:
随机阅读: