【发布时间】: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