如何在android中显示用户名Firebase

时间:2018-04-15 15:23:47

标签: java android firebase firebase-realtime-database

我已经做了很多方法来只显示firebase数据库的用户名。但我得到空值。也许你可以帮我解决这个问题。

这是我拥有的数据库。 My Firebase Database

然后这是显示用户名的活动 Activity For Display Username

这是我创建的动画,您可以更轻松地理解它。 Animated GIF

这是我的代码

FirstActivity.Java

public class FirstActivity extends AppCompatActivity {

TextView textView;
FirebaseDatabase database;
DatabaseReference reference;
Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first);

    toolbar = findViewById(R.id.my_toolbar);
    setSupportActionBar(toolbar);

    database = FirebaseDatabase.getInstance();
    textView = findViewById(R.id.tvselamatdatang);
    reference = database.getReference("Users");
    reference.addValueEventListener(new ValueEventListener() {
        @SuppressLint("SetTextI18n")
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String username = dataSnapshot.child("username").getValue(String.class);
            textView.setText("Welcome" + username);
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}
}

LoginActivity.Java

public class LoginActivity extends AppCompatActivity {

FirebaseDatabase database;
DatabaseReference users;

ProgressBar progressBar;
EditText editTextUsername, editTextPassword;
Button buttonLogin;
TextView textViewSignUp;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    database = FirebaseDatabase.getInstance();
    users = database.getReference("Users");
    progressBar = (ProgressBar) findViewById(R.id.progressbar);

    editTextUsername = (EditText) findViewById(R.id.editTextUsername);
    editTextPassword = (EditText) findViewById(R.id.editTextPassword);

    buttonLogin = (Button) findViewById(R.id.buttonLogin);
    buttonLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            signIn(editTextUsername.getText().toString(),
                    editTextPassword.getText().toString());
        }
    });

    textViewSignUp = (TextView) findViewById(R.id.textViewSignup);
    textViewSignUp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(LoginActivity.this, SignUpActivity.class);
            startActivity(intent);
        }
    });

}

private void signIn(final String username, final String password) {
    users.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if(dataSnapshot.child(username).exists()){
                if (!username.isEmpty()){
                    User login = dataSnapshot.child(username).getValue(User.class);
                    if (login.getPassword().equals(password)){
                        Toast.makeText(LoginActivity.this, "Success Login", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(getApplicationContext(), FirstActivity.class);
                        startActivity(intent);
                    }
                    else {
                        Toast.makeText(LoginActivity.this, "Password is Wrong", Toast.LENGTH_SHORT).show();
                    }
                }
                else Toast.makeText(LoginActivity.this, "Username is not Registered", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}
}

SignUpActivity.Java

public class SignUpActivity extends AppCompatActivity {

FirebaseDatabase database;
DatabaseReference users;

ProgressBar progressBar;
EditText editTextEmail, editTextUsername, editTextPassword;
Button btnSignUp;
TextView textViewLogin;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_up);

    database = FirebaseDatabase.getInstance();
    users = database.getReference("Users");

    progressBar = (ProgressBar) findViewById(R.id.progressbar);

    editTextEmail = (EditText) findViewById(R.id.editTextEmail);
    editTextUsername = (EditText) findViewById(R.id.editTextUsername);
    editTextPassword = (EditText) findViewById(R.id.editTextPassword);

    textViewLogin = (TextView) findViewById(R.id.textViewLogin);
    textViewLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(SignUpActivity.this, LoginActivity.class);
            startActivity(intent);
        }
    });

    btnSignUp = (Button) findViewById(R.id.buttonSignUp);

    btnSignUp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final User user = new User(editTextUsername.getText().toString(),
                    editTextPassword.getText().toString(),
                    editTextEmail.getText().toString());
            users.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if (dataSnapshot.child(user.getUsername()).exists())
                        Toast.makeText(SignUpActivity.this, "The Username is Already Exist!", Toast.LENGTH_SHORT).show();
                    else {
                        users.child(user.getUsername()).setValue(user);
                        Toast.makeText(SignUpActivity.this, "Success Register!", Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }
    });

}
}

User.Java

public class User {

private String username;
private String password;
private String email;

public User() {

}

public User(String username, String password, String email) {
    this.username = username;
    this.password = password;
    this.email = email;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}
}

4 个答案:

答案 0 :(得分:0)

尝试此操作以显示用户名:

String username = (String)dataSnapshot.child("test").child("username").getValue();

希望这有帮助!

答案 1 :(得分:0)

让用户模型成员公开!

public class User {

    public String username;
    public String password;
    public String email;

    public User() {

    }

    public User(String username, String password, String email) {
        this.username = username;
        this.password = password;
        this.email = email;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
    }

答案 2 :(得分:0)

你可以试试这个:

reference.addValueEventListener(
    new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // for example: if you're expecting your user's data as an object of the "User" class.
            User user = dataSnapshot.getValue(User.class);
            String username = user.getUsername();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            // read query is cancelled.
        }
    });

答案 3 :(得分:0)

 database = FirebaseDatabase.getInstance();
    reference .addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            String username = (String) dataSnapshot.child(auth.getCurrentUser().getUid()).child("basics")
                    .child("username").getValue(String.class);
            if (username == null)
                userName.setText("Hello, Anonymous");
             else
            userName.setText("Hello, "+username);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });