简体   繁体   English

如何使用 AndroidStudio 中的 Java 方法从 Firebase 实时数据库读取值?

[英]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.

相关问题 如何从 firebase 实时数据库中检索两个不同的值并在 onDataChange 方法之外使用? - How can I retrieve two different values from firebase realtime database and use outside onDataChange method? 如何通过 POJO 类从 Firebase 实时数据库中读取嵌套父节点的值? - How do I read values from a firebase realtime database for nested parent nodes via POJO classes? 如何读取 Firebase 实时数据库中的嵌套数据(哈希图) - How can I read nested data (hashmap) in Firebase RealTime Database 我如何在需要时而不只是在 DataChange 时从 firebase 实时数据库中读取数据 - how can i read data from firebase realtime database when i want and not just when DataChange 如何从 Firebase 实时数据库读取数据 - How to read data from the Firebase realtime database 如何使用 Java 从 Firebase 实时数据库中删除数据? - How to delete data from a Firebase realtime database using Java? 如何从 Firebase 实时数据库中检索值? - How can I retrieve the value from Firebase Realtime database? 我想使用来自不同类的数据库,我该如何在 JAVA 中做到这一点? (AndroidStudio/Java) - I want so use a Database from different classes, how can I do that in JAVA? (AndroidStudio/Java) 无法在 Firebase 实时数据库上读写 - can not read or write on Firebase Realtime database 从Firebase实时数据库读取数据 - Read data from firebase realtime database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM