我收到FirebaseUser ID&电子邮件如何正确登录,如调试屏幕截图所示,但DataSnapshot ds总是加载user_01信息,我努力解决这个问题,并在网站上发现类似问题,但无法提供帮助。所以有人可以帮忙解决这个问题吗?
public class AddToDatabase extends AppCompatActivity {
private static final String TAG = "AddToDatabase";
private ListView userInfolist;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
String userId;
UserInformations uInfo = new UserInformations();
DatabaseReference myRef;
FirebaseDatabase mFirebaseDatabase;
FirebaseUser user;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_to_database);
userInfolist = (ListView) findViewById(R.id.userInfolist);
mAuth = FirebaseAuth.getInstance();
mFirebaseDatabase = FirebaseDatabase.getInstance();
myRef = mFirebaseDatabase.getReference();
user = mAuth.getCurrentUser();
userId = user.getUid();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
toastMassege(" You are successfly signed in with " + user.getEmail());
} else {
Log.d(TAG, "onAuthStateChanged:signed_out");
toastMassege(" You are successfly signed out ");
}
}
};
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
showData(dataSnapshot);
}
@Override
public void onCancelled(DatabaseError error) {
}
});
}
Boolean signedstate = true;
private void showData(DataSnapshot dataSnapshot) {
ArrayList<String> arrayList = new ArrayList<>();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
if (signedstate) {
uInfo.setName(ds.getValue(UserInformations.class).getName());
uInfo.setEmail(ds.getValue(UserInformations.class).getEmail());
uInfo.setPhone_num(ds.getValue(UserInformations.class).getPhone_num());
arrayList.add(uInfo.getName());
arrayList.add(uInfo.getEmail());
arrayList.add(uInfo.getPhone_num());
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrayList);
userInfolist.setAdapter(adapter);
toastMassege("its Done ");
signedstate = false;
}
}
}
@Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
public void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
private void toastMassege(String massege) {
Toast.makeText(this, massege, Toast.LENGTH_SHORT).show();
}
}
这是类UserInformations:
class UserInformations {
public String email,name,phone_num ;
UserInformations() {
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone_num() {
return phone_num;
}
public void setPhone_num(String phone_num) {
this.phone_num = phone_num;
}
}
答案 0 :(得分:0)
您的问题是您正在for循环中创建适配器。为了解决你的问题,你需要移动这一行:
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrayList);
在for循环之外的因为每次迭代时都在创建ArrayAdapter
的新实例。请使用以下代码:
private void showData(DataSnapshot dataSnapshot) {
ArrayList<String> arrayList = new ArrayList<>();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
if (signedstate) {
uInfo.setName(ds.getValue(UserInformations.class).getName());
uInfo.setEmail(ds.getValue(UserInformations.class).getEmail());
uInfo.setPhone_num(ds.getValue(UserInformations.class).getPhone_num());
arrayList.add(uInfo.getName());
arrayList.add(uInfo.getEmail());
arrayList.add(uInfo.getPhone_num());
signedstate = false;
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrayList);
userInfolist.setAdapter(adapter);
toastMassege("its Done ");
}