Python中文网

一个关于 编程问题的解答网站.

有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java当用户登录时,如何从Firebase数据库中填写edittext

The structure所以我想在每次用户登录时将数据从Firebase数据库检索到EditText,这是我的代码。我目前有一个值事件监听器,可以从数据库中填充edittext,但它似乎不起作用。我想我把它放错地方了,而且我是安卓 studio的新手,所以我真的被卡住了,不知道该怎么办

private Button mLogin;
private EditText mEmail, mPassword, mCoins;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener firebaseAuthStateListener;
private DatabaseReference mDatabaseCoins;
private String userId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mAuth = FirebaseAuth.getInstance();
firebaseAuthStateListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
final FirebaseUser user = 
FirebaseAuth.getInstance().getCurrentUser();
if (user !=null){
Intent intent = new Intent(LoginActivity.this, 
MainActivity.class);
startActivity(intent);
finish();
return;
}
}
};
mDatabaseCoins = FirebaseDatabase.getInstance().getReference().child("Users").child(userId).child("coins");

mCoins = (EditText) findViewById(R.id.coins);
userId = mAuth.getCurrentUser().getUid();
mLogin = (Button) findViewById(R.id.login);
mEmail = (EditText) findViewById(R.id.email);
mPassword = (EditText) findViewById(R.id.password);
mLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String email = mEmail.getText().toString();
final String password = mPassword.getText().toString();
mAuth.signInWithEmailAndPassword(email, 
password).addOnCompleteListener(LoginActivity.this, new 
OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if(!task.isSuccessful()){
                        Toast.makeText(LoginActivity.this, "sign in error", 
Toast.LENGTH_SHORT).show();
                    }
                }
            });

`

下面是事件侦听器:

mDatabaseCoins.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    mCoins.setText(dataSnapshot.getValue(String.class));
                }
                @Override
                public void onCancelled(DatabaseError databaseError) {
                }
            });
        }
    });
}
@Override
protected void onStart() {
    super.onStart();
    mAuth.addAuthStateListener(firebaseAuthStateListener);
}
@Override
protected void onStop() {
    super.onStop();
    mAuth.removeAuthStateListener(firebaseAuthStateListener);
}
}

`


共 (1) 个答案

  1. # 1 楼答案

    首先:

    mDatabaseCoins = FirebaseDatabase.getInstance().getReference().child("Users").child(userId);
    

    然后:

    mDatabaseCoins.addValueEventListener(new ValueEventListener() {
              @Override
              public void onDataChange(DataSnapshot dataSnapshot) {
                   String coins = (String) dataSnapshot.child("coins").getValue();
                   mCoins.setText(coins);
              }  
            ...
    

    或者我建议您创建数据类:
    在我的另一个答案中有更多关于它的信息:https://stackoverflow.com/a/48868970/5272951