如何从onDataChange方法中获取数据?

问题描述 投票:0回答:4

情况:我想将我的函数参数中的url与从firebase实时数据库中检索到的url进行比较。然而,检索工作,我不知道如何从我的if-else语句中的String比较中获取布尔值,其中我得到一个错误:“不能将值赋给最终变量'bool'”。

compare()代码:

private boolean compare(String url) {
    mDatabaseReference = mFirebaseDatabase.getReference("Executive Branch");
    final String newUrl = url;
    final boolean bool = false;

    mDatabaseReference.limitToFirst(1).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.getChildrenCount() > 0) {
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    ExecutiveOrder executiveOrder = snapshot.getValue(ExecutiveOrder.class);
                    if (executiveOrder.getUrl().equals(newUrl)) {
                        bool = true;
                    } else {
                        bool = false;
                    }
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.e("Executive Order", "The read failed: " + databaseError.getDetails());
        }
    });

    return bool;
}
android firebase firebase-realtime-database
4个回答
0
投票

您无法为最终变量分配新值。

如果要访问内部类中的外部类变量,则必须声明final。

一种简单但不优雅的方法是声明一个大小为1的布尔数组。

mDatabaseReference = mFirebaseDatabase.getReference("Executive Branch");
final String newUrl = url;
final boolean[] bool = new boolean[1];   //boolean array of size 1

mDatabaseReference.limitToFirst(1).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.getChildrenCount() > 0) {
            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                ExecutiveOrder executiveOrder = snapshot.getValue(ExecutiveOrder.class);
                if (executiveOrder.getUrl().equals(newUrl)) {
                    bool[0] = true;   //access the bool value like this
                } else {
                    bool[0] = false;
                }
            }
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.e("Executive Order", "The read failed: " + databaseError.getDetails());
    }
});

顺便说一句,你犯了一个大错误。你的代码会一直返回false。

Firebase数据库方法是异步的,意味着它们在一个单独的线程中执行,因此我们无法阻止何时从数据库中检索数据。这取决于您的互联网速度。

所以你必须在onDataChange方法中调用适当的方法。示例......

private boolean compare(String url) {
    mDatabaseReference = mFirebaseDatabase.getReference("Executive Branch");
    final String newUrl = url;

    mDatabaseReference.limitToFirst(1).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.getChildrenCount() > 0) {
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    ExecutiveOrder executiveOrder = snapshot.getValue(ExecutiveOrder.class);
                    boolean bool = executiveOrder.getUrl().equals(newUrl);
                    doSomething(bool); //This method will handle the logic
                }
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.e("Executive Order", "The read failed: " + databaseError.getDetails());
        }
    });
}

0
投票

错误告诉你是什么问题。您正在尝试为最终变量赋值。尝试直接返回true或false。但请注意firebase调用是异步的,这意味着它可能会返回方法底部的默认值。

private boolean compare(String url) {
    mDatabaseReference = mFirebaseDatabase.getReference("Executive Branch");
    final String newUrl = url;
    final boolean bool;

    mDatabaseReference.limitToFirst(1).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.getChildrenCount() > 0) {
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    ExecutiveOrder executiveOrder = snapshot.getValue(ExecutiveOrder.class);
                    if (executiveOrder.getUrl().equals(newUrl)) {
                        bool = true;
                    } else {
                        bool = false;
                    }
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.e("Executive Order", "The read failed: " + databaseError.getDetails());
        }
    });

    if(bool!=null)        < Add a condition to check if bool is null or not
    return bool;
    else return false;
}

0
投票

使变量bool成为类中的全局变量,并删除final关键字,以便您可以从方法内的任何位置读取和写入值。


0
投票

如果要使用onDataChange方法中的变量,请在onDataChange中的方法中编写要对该变量执行的所有进程。此方法应该具有String作为参数(因为我们主要从firebase接收值作为字符串)。现在,在onDataChange方法中调用此方法,并将onDataChange的变量作为参数传递给此方法。

例如:

public void onDataChange(DataSnapshot dataSnapshot) {
   String firebaseVariable=dataSnapshot.getValue().toString();
   methodToProcess(firebaseVariable);
}

methodToProcess(String firebaseVariable){
//Your processing here
}
© www.soinside.com 2019 - 2024. All rights reserved.