如何从firebase检索特定的数据列表 [英] How to retrieve specific list of data from firebase
问题描述
我是 firebase
和 nosql
数据库的新手
I am new to firebase
and nosql
database
我正在尝试从 Firebase 检索一些特定数据.我有一个节点 universities
并且该节点有许多唯一标识符作为节点,其中包含更多数据.我想从每个节点检索 name
.
I am trying to retrieve some specific data from a Firebase. I have a node universities
and that node has many unique identifier as node which has so more data into it. I want to retrieve name
from each node.
请看看这个.
到目前为止我尝试过的:我尝试使用 addChildEventListener
但这仅侦听第一个孩子.我已经调试过它,它只显示了 universities
节点的第一个子节点的值.
What I have tried so far: I tried using addChildEventListener
but this only listens first child. I've debugged it and it was only showing the values of the first child of universities
node.
myRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
List<University> university = new ArrayList<>();
Map<String, Object> td = (HashMap<String, Object>) dataSnapshot.getValue();
Log.d("TAG",dataSnapshot.getValue().toString());
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
我也尝试过 addValueEventListener
.这会侦听整个节点并返回整个数据,但我无法为其提取名称",因为它包含唯一标识符.
I have also tried addValueEventListener
. This listens the whole node and return whole data, but I am unable to extract "name" for it since it contains unique identifiers.
请指导我走向正确的方向.
Please guide me into the right directions.
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
University university = dataSnapshot.getValue(University.class);
List<University> universities = (List<University>) dataSnapshot.getValue(University.class);
*//*List<String> lst = new ArrayList<String>();
for(DataSnapshot dsp : dataSnapshot.getChildren()){
lst.add(dsp.getKey());
}
Log.d("TAG",lst.toString());*//*
Map<String, Object> td = (HashMap<String, Object>) dataSnapshot.getValue();
*//*List<Object> values = new ArrayList<Object>(td.values());
List<String> list=new ArrayList<String>();
for(int i=0; i<values.size(); i++){
list.add((String) values.get(i));
}*//*
Log.d("TAG", university.toString());
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
推荐答案
如果您使用 addValueEventListener
从 Firebase 检索所有大学
If you using addValueEventListener
for retrieve all university from Firebase
List<University> universityList = new ArrayList<>();
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
universityList.clear();
for (DataSnapshot postSnapshot: snapshot.getChildren()) {
University university = postSnapshot.getValue(University.class);
universityList.add(university);
// here you can access to name property like university.name
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + firebaseError.getMessage());
}
});
或者如果你使用 addChildEventListener
List<University> universityList = new ArrayList<>();
myRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
University university = dataSnapshot.getValue(University.class);
universityList.add(university);
Log.i(TAG,"add university name = " + university.name);
...
}
...
}
这篇关于如何从firebase检索特定的数据列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!