【问题标题】:Android Firebase addListenerForSingleValueEvent pass valueAndroid Firebase addListenerForSingleValueEvent 传值
【发布时间】:2018-12-06 05:22:09
【问题描述】:

我在片段中有按钮,单击该按钮应检查数据是否存在于 firebase db 中。下面是一个单独的类文件中的函数,它将在异步任务中的按钮单击时调用。

如何将布尔值 true/false 从 addListenerForSingleValueEvent 返回到片段异步任务?

void checkDataExists(final String mobile){
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.
                }

                 @Override
                 public void onCancelled(@NonNull DatabaseError databaseError) {
                     Log.w(TAG + "/checkDataExists","Data read from DB failed: " + databaseError.getMessage());
                 }
             });
         }

【问题讨论】:

    标签: android firebase firebase-realtime-database


    【解决方案1】:

    我也有这样的情况,我创建了自己的回调,例如:

    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()跨度>
    猜你喜欢
    • 1970-01-01
    • 2018-08-11
    • 1970-01-01
    • 2016-04-01
    相关资源
    最近更新 更多