如何迭代所有子节点并检索ListView中所有子节点的键值

时间:2017-07-01 14:53:40

标签: android firebase-realtime-database

public class PendingReqFragment extends android.app.Fragment {



public PendingReqFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView =inflater.inflate(R.layout.pending_req, container, false);
    return rootView;

}

我想从sahil节点的所有子节点检索所有查询名称,并在ListView中动态显示。

这是数据库的screenshot

{
    "query" : {
    "sahil" : [ null, {
      "address" : "bdbdbdb",
      "name" : "sahildeep",
      "no" : "64649",
      "pincode" : "373737",
      "query" : "hdbdbdvd",
      "user_name" : "sahil"
    }, {
      "address" : "zbxbbx",
      "name" : "sahil mehra",
      "no" : "949495",
      "pincode" : "1345",
      "query" : "snxbxb",
      "user_name" : "sahil"
    } ]
  }
}

1 个答案:

答案 0 :(得分:0)

您需要在数据库中创建对子项的引用:

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myPostReference = database.getReference("message").child("child1");

然后在该引用上设置ValueEventListener以获取DataSnapshot:

ValueEventListener postListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    // Get Post object and use the values to update the UI
    Post post = dataSnapshot.getValue(Post.class);
    // ...
}

@Override
public void onCancelled(DatabaseError databaseError) {
    // Getting Post failed, log a message
    Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
    // ...
}
};
mPostReference.addValueEventListener(postListener);

Firebase docs

中有更详细的信息