If 语句,Android Studio with Firebase

If statement, Android Studio with Firebase

所以我试图将数据发送回我的 Firebase 数据库,只有已经注册并登录的用户。我已经编写了一个代码来发送数据,但我正在尝试编写一个 else if 语句来表示一条消息在这种情况下,如果他们尚未注册,则会出现 Toast。我正在努力为此编写代码。

private void sendScore() {

    String userName = name.getText().toString();
    String userScore = BenchScore.getText().toString();

    if(!TextUtils.isEmpty(userName) && !TextUtils.isEmpty(userScore)) {

        String id = databaseReference.push().getKey();

        ScoreProfile scoreProfile = new ScoreProfile(id, userName, userScore);

        databaseReference.child(FirebaseAuth.getInstance().getUid()).setValue(scoreProfile);
        name.setText("");
        BenchScore.setText("");

    } else if(FirebaseAuth.getInstance().) {
        Toast.makeText(Score.this, "You need to sign up in order to use this", Toast.LENGTH_SHORT).show();
    }
}

如果你想检查用户是否登录,你可以使用:

...
else if( FirebaseAuth.getInstance().getCurrentUser() == null ) 
     {
     // Call your toast here
     }

用这个替换你的代码:

private void sendScore() {

    String userName = name.getText().toString();
    String userScore = BenchScore.getText().toString();

    if(FirebaseAuth.getInstance().getUid() != null && !TextUtils.isEmpty(userName) && !TextUtils.isEmpty(userScore)){

        String id = databaseReference.push().getKey();

        ScoreProfile scoreProfile = new ScoreProfile(id, userName, userScore);

        databaseReference.child(FirebaseAuth.getInstance().getUid()).setValue(scoreProfile);
        name.setText("");
        BenchScore.setText("");

    } else if (FirebaseAuth.getInstance().getUid() == null) {
        Toast.makeText(Score.this, "You need to sign up in order to use this", Toast.LENGTH_SHORT).show();
    }
}

我刚刚在 if 语句和 else if 中检查了 getUid() 是否为非空,我检查了 getUid() 是否为空.希望这能达到您的目的。