如何在用户登录时从Firebase数据库填充edittext

时间:2018-02-26 15:00:28

标签: java android firebase firebase-realtime-database

The structure因此,我希望每次用户登录时都将Firebase数据库中的数据检索到EditText,这是我的代码。我目前有一个值事件监听器,它从数据库填充edittext但它似乎没有用。我想我把它放在错误的地方或者其他什么东西,我也是Android工作室的新手,所以我真的卡住了,我不知道该怎么做。

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);
}
}

`

2 个答案:

答案 0 :(得分:0)

要实现此目的,请使用以下代码:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("Users").child(uid);
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        EditText mCoins = (EditText) findViewById(R.id.coins);
        mCoins.setText(dataSnapshot.child("coins").getValue(String.class));
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
uidRef.addListenerForSingleValueEvent(eventListener);

答案 1 :(得分:0)

第一:

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