https://i.stack.imgur.com/HHuaO.png
I wonder if there's any way where when I try to retrieve the specific email, I get the other child value which is the id. For example, I retrieve the [email protected], I get the id in the database for return. Whenever i tried to access the id, it says cannot resolve symbol id. Intent doesnt work if i put it inside the if()
public class loginActivity extends AppCompatActivity {
CheckBox checkbox;
EditText email;
EditText password;
FirebaseAuth mAuth;
String emailText;
String passwordText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
checkbox = findViewById(R.id.checkbox);
email = findViewById(R.id.email);
password = findViewById(R.id.password);
}
public void confirm(View view) {
checkAccount();
}
private void checkAccount() {
emailText = email.getText().toString();
passwordText = password.getText().toString();
mAuth = FirebaseAuth.getInstance();
checkBox();
mAuth.signInWithEmailAndPassword(emailText, passwordText).addOnCompleteListener(task -> {
if (task.isSuccessful()) {
if (!checkbox.isChecked()) {
Toast.makeText(loginActivity.this, "Account signin successful", Toast.LENGTH_SHORT).show();
goToMain(new DataRetriever() {
@Override
public void onIdFound(String id) {
Intent toMain = new Intent(loginActivity.this, MainActivity.class);
toMain.putExtra("welcomeId", id);
startActivity(toMain);
}
});
}
} else {
Toast.makeText(loginActivity.this, "Account signin failed: " task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
public void goToMain(DataRetriever retriever) {
DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference();
dbRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot snapshot1: snapshot.getChildren()){
String email = String.valueOf(snapshot1.child("email").getValue());
if (email.equals(emailText)){
String id = String.valueOf(snapshot1.child("id").getValue());
retriever.onIdFound(id);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
public interface DataRetriever{
void onIdFound(String id);
}
public void checkBox() {
if (checkbox.isChecked()) {
SharedPreferences sharedPreferences = getSharedPreferences("remember", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("remember", "true");
editor.apply();
Intent toMain = new Intent(loginActivity.this, introActivity.class);
startActivity(toMain);
}
}
}
CodePudding user response:
You mean that you want to get the other childs instead of the current you have, yes it is possible
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
reference.child("users").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot snapshot:dataSnapshot.getChildren()){
String email= snapshot.child("email").getValue(String.class);//get email
//Check if email is the not same
if(!email.Equals(EditTextValue){
//other value id
String id= snapshot.child("id").getValue(String.class);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
CodePudding user response:
Add this interface
public interface DataRetriever{
void onIdFound(String id);
}
Replace your gotoMain
with this
public void goToMain(DataRetriever retriever ) {
DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference().child("users");
dbRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot snapshot1: snapshot.getChildren()){
String email = String.valueOf(snapshot1.child("email").getValue());
if (email.equals(emailText)){
String id = String.valueOf(snapshot1.child("id").getValue());
retriever.onIdFound(id);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
Call gotoMain
like below:
goToMain(new DataRetriever() {
@Override
public void onIdFound(String id) {
//do whatever you want to do with id
}
});