从简单的FireBase数据库读取 [英] Reading from a simple FireBase Database
问题描述
我有一个非常简单的布局
$ lot $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ :low
}
}
当然MyAppName {}以上全部。
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getInstance()。getReference();
//从数据库读取
myRef.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
/ /这个方法被初始值调用一次,并且
//每当这个位置的数据被更新。
lotMap =(HashMap)dataSnapshot.getValue();
Log .d([Directions Activity]:,Lot1 value ======+ lotMap.get(lot));
Iterator it = lotMap.entrySet()。iterator();
while(it.hasNext()){
Map.Entry pair =(Map.Entry)it.next();
Log.d([Directions Activity]:,iterator + pair.getKey()+=+ pair.getValue());
System.out.println();
it.remove(); //避免ConcurrentModificationException异常
}
$ p $这些是从log返回的结果 D / [Directions Activity] :: Lot1 value ====== null // null显然
//因为密钥lot1不存在
D / [Directions Activity] :: lot = {lot3 = low,lot2 = low,lot1 = low}
对我来说,它看起来像是返回字符串{lot3 = low,lot2 = low,lot1 = low},但我希望能够获得一个数组,每个价值如果可能。
这是可以实现的吗?
解决方案你的代码有一些调整。您的
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getInstance()。getReference();
应该像这样写,
DatabaseReference myRef = FirebaseDatabase.getInstance()。getReference();
DatabaseReference数据库= myRef.child(anyValueNameYouSpecifyInConsole);
这2行应该在onCreate方法之外声明。你需要用addValueEventListener指定的是第二个DatabaseReference,而不是第一个。所以,它应该看起来像这样从我的例子,
database.addValueEventListener(new ValueEventListener)
$ c
$ p
$ b 它会导入方法。在一个特定的TextView中,然后只是findViewById你想使用的TextView,并将其包含在onDataChange方法中,像这样,
String x = dataSnapshot .getValue(String.class);
textViewNameDeclared.setText(x);
不要忘记更改安全规则以读取。
I'm having some issues reading from a Firebase Database.
I have a pretty simple layout
{
"lot" : {
"lot1" : "low",
"lot2" : "low",
"lot3" : "low"
}
}
Of course MyAppName { } is above this all.
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getInstance().getReference();
// Read from the database
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
lotMap = (HashMap) dataSnapshot.getValue();
Log.d("[Directions Activity]: ", "Lot1 value ====== " +lotMap.get("lot"));
Iterator it = lotMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
Log.d("[Directions Activity]: ", "iterator " + pair.getKey() + " = " + pair.getValue());
System.out.println();
it.remove(); // avoids a ConcurrentModificationException
}
}
Here's what returns from log
D/[Directions Activity]:: Lot1 value ====== null //null obviously
//because key lot1 doesn't exist
D/[Directions Activity]:: lot = {lot3=low, lot2=low, lot1=low}
So to me, it looks like it's returning the string {lot3=low, lot2=low, lot1=low}, but I'd like to be able to get an array, with each value if possible.
Is this achievable??
解决方案 There's some tweak in your code. Your
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getInstance().getReference();
should be write like this,
DatabaseReference myRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference database = myRef.child("anyValueNameYouSpecifyInConsole");
Those 2 lines should be declare outside onCreate method. The one that you need to specify with addValueEventListener is the 2nd DatabaseReference, not the first one. So, it should looks like this from my example,
database.addValueEventListener (new ValueEventListener)
and it will import method.
If you wish the data to be displayed in a particular TextView, then just findViewById the TextView you wanna use and include it in onDataChange method like so,
String x = dataSnapshot.getValue(String.class);
textViewNameDeclared.setText(x);
And don't forget to change the security rule for reading.
这篇关于从简单的FireBase数据库读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!