当前位置: 首页 > 知识库问答 >
问题:

如何在android中显示用户名Firebase

聂翼
2023-03-14

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

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

然后这是显示用户名的活动显示用户名的活动

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

这是我的密码

第一个活动.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) {

        }
    });
}
}

注册活动.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) {

                }
            });
        }
    });

}
}

用户.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;
}
}

共有3个答案

闻修筠
2023-03-14

您可以尝试以下操作:

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.
        }
    });
陈毅
2023-03-14

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

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

希望这有帮助!

申炳
2023-03-14

公开用户模型成员!

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;
    }
    }
 类似资料:
  • 我试图将我当前用户的用户名添加到我的URL(route)中,但是我不断地得到错误。我已经尝试了很多下面提到的方法,但是没有一个有效。以下是我的代码: Layout.html: 当我尝试转到另一个页面时,我收到错误,当我转到设置时收到404。 jinja2.exceptions.UndefinedError jinja2.exceptions.UndefinedError:'user'未定义 MOD

  • 我在登录后从URL中获取响应,但我只能在我的控制台中打印数据,但如何从该响应中获取邮件和名称 我已经尝试了未来的回应,但当我得到一个未来的回应,但它返回错误 LoginPage.dart Homepage.dart 请任何人可以帮助显示姓名和邮箱抽屉 这是我的Json 在此处查找 Json 文件

  • 在我的问题中,我使用NetBeans创建了单独的jframes。一个是使用textfield输入用户名和密码字段的登录框架。一旦登录,你就会进入下一个框架,回家。现在,我在home框架上放置了一个标签,以显示登录用户的名称。现在我想知道如何获得应用在登录框中的用户名来显示在这个标签中。提前感谢…在这方面的任何帮助都是非常感谢的。谢了。我想从登录框架中获取登录用户名以显示在主框架中。我尝试了以下代码

  • 我试着使用这种方法:Android-登录textView后显示sqlite数据库中的用户名 但我得到一个错误: 我想要的是在成功登录后在UserPanelActivity中显示当前昵称。 用户面板活动: DataBaseHelper: UserPanel.xml 登录活动: 谢谢你的帮助!:)

  • 我正在尝试用android显示PPT文件。我从ApachePOI开始,bcoz我还没有找到任何免费的开源jar。我从将ppt幻灯片转换为图像开始,参考此链接将PowerPoint幻灯片导出为java。awt。图表2D 这是Java语言。我找不到Dimension、BuffereImage和Graphics2D类。我已经导入了poi-scratchpad-3.8-20120326。jar在我的构建路