我想在辅助类中使用静态方法来检查返回类型为boolean的firebase中的引用是否存在。 但是“ boolean isExist”必须是最后的才能访问嵌套类“ onDataChange”,因此我无法在此嵌套类中更改其值。 如何最好地解决这个问题?
public static boolean checkExist(final Context context, String id) {
final boolean isExist = false;
mEventsDatabaseReference.child(id).addListenerForSingleValueEvent(
new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
isExist = true;
} else {
isExist = false;
Toast.makeText(context, "Sorry, this coverage was deleted.", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
return isExist;
}