[英]How can I read values from Firebase Realtime Database by using a Java method in AndroidStudio?
My Firebase Realtime Database has been built by loading an object of the Java class HashMap.我的 Firebase 实时数据库是通过加载 Java 类 HashMap 的对象构建的。 In my Android Studio app I'm trying to write a method that takes a String (the key) as input, searches through the database and if the string is found it returns the associated Float (the value), otherwise it returns 0. How can I do this?在我的 Android Studio 应用程序中,我试图编写一个以字符串(键)作为输入的方法,搜索数据库,如果找到字符串,则返回关联的浮点数(值),否则返回 0。如何我可以这样做吗? Any help would be really appreciated!任何帮助将非常感激!
EDIT: I've tried to follow the suggestions, adapting them to my particular case, but I didn't manage to solve the problem yet.编辑:我试图遵循这些建议,使它们适应我的特定情况,但我还没有设法解决问题。
I wrote the following code in MainActivity:我在 MainActivity 中编写了以下代码:
DatabaseReference myRef;
Float tempValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
myRef = FirebaseDatabase.getInstance().getReference("myRoot");
tempValue=0f;
...
}
public void retrieveValueFromDatabase(String childName, final MainActivity activity){
myRef.child(childName).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Float value=dataSnapshot.getValue(Float.class);
if (value==null){
value=0f;
}
activity.tempValue=value;
//First Toast
//Toast.makeText(activity,"tempValue = "+tempValue.toString(), Toast.LENGTH_LONG).show();
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
}
public void useValues(){
retrieveValueFromDatabase(childName,this);
//Second Toast
//Toast.makeText(this,"tempValue = "+tempValue.toString(), Toast.LENGTH_LONG).show();
//code using tempValue from here
...
}
If I uncomment the first toast, the correct value inside tempValue
is shown, but if I uncomment the second toast, the value of tempValue
shown is the default one (0.0).如果取消注释第一个 toast,则会显示tempValue
的正确值,但如果取消注释第二个 toast,则显示的tempValue
值是默认值 (0.0)。 What am I missing?我错过了什么?
You need to use addValueEventListener
to retrieve data from the database:您需要使用addValueEventListener
从数据库中检索数据:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("myRoot").orderByChild("name").equalTo("peter");
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.i("Database", dataSnapshot.child("floatValue").getValue(Long.class));
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
})
Here, you add a reference to the root node, then query using equalTo()
to check if name = peter
exists in the database and return the float value.在这里,您添加对根节点的引用,然后使用equalTo()
查询以检查数据库中是否存在name = peter
并返回浮点值。
You should read the guide:你应该阅读指南:
https://firebase.google.com/docs/database/android/read-and-write https://firebase.google.com/docs/database/android/read-and-write
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.