So I'm currently trying to make a Firebase database for an Android Studio app consisting of multiple UserProfile
Class Objects, each containing 4 properties, that is username, password, secretQuestion and secretAnswer. I can add these objects to database with each having a unique key, having no problem with that but I need to check every one of them when signing in. I made a retrieving method with ValueEventListener that returns a UserProfile list. When I click sign-in in my login page the method always returns a null list and after restarting the app or playing with some pages, app crashes when I try to sign in. I can't find the problem with the method unfortunately...
Here is my method for getting the data:
public static List<UserProfile> readUserProfile()
{
final List<UserProfile> profiles = new ArrayList<UserProfile>();
DatabaseReference getProfile;
getProfile = CreateProfile1.database.getReference();
getProfile.child( "UserProfiles").addValueEventListener( new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Iterable<DataSnapshot> children = dataSnapshot.getChildren();
for ( DataSnapshot child : children )
{
UserProfile aProfile = child.getValue( UserProfile.class);
profiles.add( aProfile);
}
}
@Override
public void onCancelled(DatabaseError databaseError)
{
Log.e("The read failed: ", databaseError.getMessage());
}
});
return profiles;
}
And here is my sign-in checking:
loginProfiles = CreateProfile2.readUserProfile();
for ( int i = 0; i < loginProfiles.size(); i++ )
{
if ( loginProfiles.get( i).getUsername().equals( usernameField.getText().toString() ) &&
loginProfiles.get( i).getPassword().equals( passwordField.getText().toString() ) )
{
Intent goToMainPage;
goToMainPage = new Intent(LoginPage.this, MainMenu.class);
startActivity(goToMainPage);
}
}