英文:
Reading from a Firebase Database
问题
我试图从 Firebase 数据库中读取数据,我已经阅读并在各处查找过,但我陷入了死胡同。
以下是我所做的一切。
依赖项:
implementation 'com.google.firebase:firebase-storage:9.2.1'
implementation 'com.google.firebase:firebase-database:9.2.1'
implementation 'com.google.firebase:firebase-auth:9.2.1'
implementation 'com.google.firebase:firebase-core:9.2.1'
minSdkVersion:15
DatabaseReference mDatabase;
mDatabase = FirebaseDatabase.getInstance().getReference();
然后在按钮的 onClick 方法中,我添加了监听器:
mDatabase.child("List").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String savedData = dataSnapshot.getValue(String.class);
Log.d(TAG, "snapshot: " + savedData);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, "Error");
}
});
这是数据库的样式。
非常感谢您的帮助。
英文:
I'm trying to read from a Firebase Database, I've read and looked everywhere, but I'm at a dead end.
Here's all that I've done.
Dependencies:
> implementation 'com.google.firebase:firebase-storage:9.2.1'
>
> implementation 'com.google.firebase:firebase-database:9.2.1'
>
> implementation 'com.google.firebase:firebase-auth:9.2.1'
>
> implementation 'com.google.firebase:firebase-core:9.2.1'
minSdkVersion: 15
DatabaseReference mDatabase;
mDatabase = FirebaseDatabase.getInstance().getReference();
then in a Button onClick method, I put the listener:
mDatabase.child("List").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String savedData = dataSnapshot.getValue(String.class);
Log.d(TAG, "snapshot: " + savedData);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, "Error");
}
});
Here is a look at the Database.
Would appreciate the input.
答案1
得分: 0
你正在尝试读取List
下的String
值。但是,在List
下面并没有字符串值,而是有一组对象。要获取实际的值,您需要在您的代码中导航JSON结构。
类似这样的代码:
DatabaseReference mDatabase;
mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.child("List").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot tierSnapshot: dataSnapshot.getChildren()) {
Log.d(TAG, tierSnapshot.getKey()); // "Tier 1", "Tier 1 B"
for (DataSnapshot secondSnapshot: tierSnapshot.getChildren()) {
Log.d(TAG, secondSnapshot.getKey()); // "Tier 2", "Tier 2 B"
String str = secondSnapshot.getValue(String.class);
Log.d(TAG, str); // null, "2 B"
Long num = secondSnapshot.getValue(Long.class);
Log.d(TAG, num); // 2, null
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.w(TAG, "Error", databaseError);
}
});
另一个需要注意的是,您的值是不同类型的,第一个是数字2
,而第二个是字符串"2 B"
。因此有两个getValue()
调用,以获取特定的类型。您也可以只使用secondSnapshot.getValue()
,然后按照通常的方式处理生成的对象(例如,在其上调用toString()
)。
英文:
You're trying to read a String
value under List
. But right under List
there's no string value, rather there's a list of objects under there. To get the actual values, you'll need to navigate the JSON structure in your code.
Something like:
DatabaseReference mDatabase;
mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.child("List").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot tierSnapshot: dataSnapshot.getChildren()) {
Log.d(TAG, tierSnapshot.getKey(); // "Tier 1", "Tier 1 B"
for (DataSnapshot secondSnapshot: tierSnapshot.getChildren()) {
Log.d(TAG, secondSnapshot.getKey(); // "Tier 2", "Tier 2 B"
String str = secondSnapshot.getValue(String.class);
Log.d(TAG, str); // null, "2 B"
Long num = secondSnapshot.getValue(long.class);
Log.d(TAG, num); // 2, null
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.w(TAG, "Error", databaseError);
}
});
An extra thing to note is that your values are of a different type, with the first one being the number 2
, while the second one is a string "2 B"
. Hence the two getValue()
calls, to get the specific type out of there. You could also just do secondSnapshot.getValue()
and deal with the resulting object as you'd usually do (e.g. call toString()
on it).
专注分享java语言的经验与见解,让所有开发者获益!
评论