【问题标题】:Firebase addListenerForSingleValueEvent wont work when for loop is added添加 for 循环时,Firebase addListenerForSingleValueEvent 将不起作用
【发布时间】:2022-01-06 13:46:29
【问题描述】:

我想登录两种不同的用户类型。所以我这样编码。但是 addListenerForSingleValueEvent 中的 for 循环在这里不起作用,请帮助。它就像它不会进入 for 循环。请帮助我陷入困境 登录.java

    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            performLogin();
        }
    });
}
private String email,password;
private void performLogin() {
    email=emaillog.getText().toString().trim();
    password=passwordlog.getText().toString().trim();
    if(!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
        Toast.makeText(this, "Invalid Email Address", Toast.LENGTH_SHORT).show();
        return;
    }
    if(TextUtils.isEmpty(password)) {
        Toast.makeText(this, "Password should not be empty", Toast.LENGTH_SHORT).show();
        return;
    }
    progressDialog.setMessage("Logging In....");
    progressDialog.show();
    firebaseAuth.signInWithEmailAndPassword(email,password)
            .addOnSuccessListener(new OnSuccessListener<AuthResult>() {
                @Override
                public void onSuccess(@NonNull AuthResult authResult) {
                    //logged in successfully
                    checkOnline();
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    //logging in failed
                    progressDialog.dismiss();
                    Toast.makeText(Login.this, ""+e.getMessage(), Toast.LENGTH_SHORT).show();

                }
            });
}

private void checkOnline() {
    //after loggin in
    progressDialog.setMessage("Checking User....");
    HashMap<String, Object> hashMap = new HashMap<>();
    hashMap.put("online","true");
    //update value to db
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Users");
    ref.child(firebaseAuth.getUid()).updateChildren(hashMap)
            .addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(@NonNull Void unused) {
                    //Update by checking user
                    checkUserType();
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    //update unsuccessfully
                    progressDialog.dismiss();
                    Toast.makeText(Login.this, ""+e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
}

private void checkUserType() {
    progressDialog.setMessage("Checking UserType....");
    //if the user is seller,then go into seller dashboard
    // if the user is customer, then go into customer dashboard
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Users");
    progressDialog.setMessage("Checking UserType1....");
    ref.orderByChild("uid").equalTo(firebaseAuth.getUid())
            .addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    **for (DataSnapshot ds: snapshot.getChildren()){
                        String accounttype =""+ds.child("accounttype").getValue();
                        if(accounttype == "Seller"){
                            startActivity(new Intent(Login.this,SellerDahboard.class));
                        }
                        else{
                            startActivity(new Intent(Login.this,SellerDahboard.class));
                        }
                    }**
                }

                @Override
                public void onCancelled(@NonNull DatabaseError error) {
                }
            });

火力基地

【问题讨论】:

    标签: java android firebase firebase-realtime-database


    【解决方案1】:

    第一步是忽略潜在错误并实施onCancelled

    至少应该如下所示:

    public void onCancelled(@NonNull DatabaseError databaseError) { 
        throw databaseError.toException(); 
    }
    

    或者:

    public void onCancelled(@NonNull DatabaseError databaseError) { 
        Log.e("Firebase", databaseError.toException());
    }
    

    如果这显示来自数据库的异常,我建议搜索该错误消息,因为它之前可能已经介绍过。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-29
      • 2019-11-30
      • 1970-01-01
      • 2016-06-25
      • 1970-01-01
      • 2017-04-13
      • 2013-12-31
      • 1970-01-01
      相关资源
      最近更新 更多