我想检索Firebase DB中子节点的子节点的所有差事名称。
但是,我只能在第二个子节点(随机密钥)之前引用 这是我的代码;
View view = inflater.inflate(R.layout.tab1,container,false);
String r = "Errands";
root = FirebaseDatabase.getInstance().getReference().child(r);
list = (ListView) view.findViewById(R.id.mylistview);
arrayAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,list_of_errands);
list.setAdapter(arrayAdapter);
root.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Set<String> set = new HashSet<String>();
Iterator i = dataSnapshot.getChildren().iterator();
while (i.hasNext()) {
set.add(((DataSnapshot) i.next()).getKey());
}
请帮助
答案 0 :(得分:1)
请使用此代码:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference errandsRef = rootRef.child("Errands");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String key = ds.getKey();
DatabaseReference keyRef = rootRef.child("Errands").child(key);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String Errand = ds.child("Errand").getValue(String.class);
Log.d("TAG", Errand);
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
keyRef.addListenerForSingleValueEvent(valueEventListener);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
errandsRef.addListenerForSingleValueEvent(eventListener);
您的输出将是:
Go to Strath
//and so on
希望它有所帮助。