Android Firebase addListenerForSingleValueEvent传递值
我在一个片段中有一个按钮,单击该按钮可以检查firebase db中是否存在数据。下面是单独类文件中的函数,在异步任务中单击按钮时将调用该函数 如何将addListenerForSingleValueEvent中的布尔值true/false返回到片段异步任务Android Firebase addListenerForSingleValueEvent传递值,android,firebase,firebase-realtime-database,Android,Firebase,Firebase Realtime Database,我在一个片段中有一个按钮,单击该按钮可以检查firebase db中是否存在数据。下面是单独类文件中的函数,在异步任务中单击按钮时将调用该函数 如何将addListenerForSingleValueEvent中的布尔值true/false返回到片段异步任务 void checkDataExists(最终字符串移动){ DatabaseReference fireDBRef=FirebaseDatabase.getInstance().getReference(context.getStrin
void checkDataExists(最终字符串移动){
DatabaseReference fireDBRef=FirebaseDatabase.getInstance().getReference(context.getString(R.string.app_name));
fireDBRef.addListenerForSingleValueEvent(新的ValueEventListener()){
@凌驾
public void onDataChange(@NonNull DataSnapshot DataSnapshot){
字符串移动=
valueOf(dataSnapshot.child(context.getString(R.String.tracked_mobile))
.getValue());
//比较字符串
布尔匹配=mobile.equals(mob);
//将匹配值返回到片段以更新视图。
}
@凌驾
已取消的公共void(@NonNull DatabaseError DatabaseError){
Log.w(TAG+“/checkDataExists”,“从数据库读取数据失败:”+databaseError.getMessage());
}
});
}
我也有这样的案例,我创建了自己的回调,如:
public interface IMyCallback {
void onSuccess(boolean isExist);
void onFailure(String error);
}
现在,当我调用函数checkDataExists
时,它看起来像:
checkDataExists(mobile, new ISingUpCallback() {
@Override
public void onSuccess(boolean isExist) {
}
@Override
public void onFailure(String error) {
}
});
在支票中,您需要进行如下更改:
void checkDataExists(final String mobile, final IMyCallback callback){
DatabaseReference fireDBRef = FirebaseDatabase.getInstance().getReference(context.getString(R.string.app_name);
fireDBRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String mob =
String.valueOf(dataSnapshot.child(context.getString(R.string.tracked_mobile))
.getValue());
//compare the strings mobile
boolean match = mobile.equals(mob);
// return match value to fragment to update the view.
callback.onSuccess(match);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
callback.onFailure(databaseError.getMessage());
}
});
}
谢谢…只有一件事…databaseError.getException()不起作用…它必须是databaseError.getMessage()或databaseError.getDetails()