【发布时间】:2019-03-22 00:12:54
【问题描述】:
情况:我想将我的函数参数中的 url 与从我的 firebase 实时数据库中检索到的 url 进行比较。检索有效,但是,我不知道如何从 if-else 语句中的字符串比较中获取布尔值,其中出现错误:“无法将值赋给最终变量 '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