Android Firebase:如何简单地获取某个键下的所有值? [英] Android Firebase: How to simply get all the values under a certain key?
问题描述
要解决这个问题,是的,SO上也有类似的问题,但是并不能完全解释我的情况对我的想法的要求.
To get it out of the way, yes there is a similar question on SO, but it doesn't explain exactly what my situation asks for I think.
在Firebase实时数据库中,我有一个看起来像这样的json树:
In the Firebase Realtime Database, I have a json tree that looks something like this:
users{
userid{
templates{
templateName1{ // example name that the user saves their template as.
// each template is an ArrayList
//templateName1 ArrayList contents
}
templateName2{
//templateName1 ArrayList contents
}
templateName2{
//templateName1 ArrayList contents
}
}
}
}
我需要能够在模板"中列出每个模板,而在其他时候,只需获取特定模板的ArrayList值即可.但是我似乎无法弄清楚如何做这个简单的事情.
I need to be able to list each template in "templates", and at other times simply get the ArrayList value of a specific template. However I can't seem to figure out how to do this simple thing.
根据我在Google中搜索/阅读的文档,我的代码应如下所示:
From what I've googled/read in the docs, my code should look something like this:
DatabaseReference mTemplateRef = mDatabase.child("users").child(uid).child("templates");
mTemplateRef.addListenerForSingleValueEvent(
new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
dataSnapshot.getChildren();
// somehow get the values here
}
});
当我调试并输入表达式数据dataSnapshot.getChildren()时,我可以看到预期的内容,但似乎无法将这些数据导入我的应用程序.有任何想法吗?
When I debug, and enter the expression data dataSnapshot.getChildren() I can see the contents as expected, but I can't seem to get that data into my app. Any ideas?
推荐答案
这就是我想要的:
settingsRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()){
// easy
String value = dataSnapshot1.getValue(String.class);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
这篇关于Android Firebase:如何简单地获取某个键下的所有值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!