我正在开发我的第一个firebase android应用程序,它几乎已经完成,但我仍然坚持从数据库中检索数据,添加和更新数据工作正常。 在我的viewInformation布局中,它没有进入showData方法的forloop(使用log cat检查) 我想将数据显示给登录的特定用户。(仅显示他/她的数据)
显示信息代码:
public class ViewInformation extends AppCompatActivity {
private static final String TAG = "ViewInformation";
private FirebaseDatabase mFirebaseDatabase;
private FirebaseAuth auth;
private FirebaseAuth.AuthStateListener authListener;
private DatabaseReference mRef;
String userID;
private ListView mListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_information);
Toast.makeText(getApplicationContext(), "Showing Details...", Toast.LENGTH_SHORT).show();
mListView = findViewById(R.id.listview);
auth = FirebaseAuth.getInstance();
mFirebaseDatabase = FirebaseDatabase.getInstance();
mRef = mFirebaseDatabase.getReference();
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); //get current user
userID = user.getUid(); //get userID of current user
authListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user == null) {
// user auth state is changed - user is null ...launch login activity
startActivity(new Intent(ViewInformation.this, LoginActivity.class));
finish();
}
}
};
mRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Toast.makeText(getApplicationContext(), "Showing Details...", Toast.LENGTH_SHORT).show();
showData(dataSnapshot);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void showData(DataSnapshot dataSnapshot) {
Toast.makeText(getApplicationContext(), "...", Toast.LENGTH_SHORT).show();
for(DataSnapshot ds:dataSnapshot.getChildren()){
userInformation uInfo = new userInformation();
uInfo.setName(ds.child(userID).child("Name").getValue(userInformation.class).getName());
uInfo.setGender(ds.child(userID).child("Gender").getValue(userInformation.class).getGender());
uInfo.setMobile_Number(ds.child(userID).child("Mobile_Number").getValue(userInformation.class).getMobile_Number());
uInfo.setAddress(ds.child(userID).child("Address").getValue(userInformation.class).getAddress());
Log.d(TAG, "showData: Name: " + uInfo.getName());
Log.d(TAG, "showData: Gender: " + uInfo.getGender());
Log.d(TAG, "showData: Mobile_Number: " + uInfo.getMobile_Number());
Log.d(TAG, "showData: Address: " + uInfo.getAddress());
ArrayList<String> array = new ArrayList<>();
array.add(uInfo.getName());
array.add(uInfo.getGender());
array.add(uInfo.getMobile_Number());
array.add(uInfo.getAddress());
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,array);
mListView.setAdapter(adapter);
}
}
@Override
public void onStart() {
super.onStart();
auth.addAuthStateListener(authListener);
}
@Override
public void onStop() {
super.onStop();
if (authListener != null) {
auth.removeAuthStateListener(authListener);
}
}
}
Getter / Setter方法:(userInformation.java)
package com.scoratech.scoraxchange;
public class userInformation {
private String Name;
private String Gender;
private String Mobile_Number;
private String Address;
public userInformation(){
}
public String getName() {
return Name;
}
public void setName(String name) {
this.Name = name;
}
public String getGender() {
return Gender;
}
public void setGender(String gender) {
this.Gender = gender;
}
public String getMobile_Number() {
return Mobile_Number;
}
public void setMobile_Number(String mobile_Number) {
this.Mobile_Number = mobile_Number;
}
public String getAddress() {
return Address;
}
public void setAddress(String address) {
this.Address = address;
}
}
答案 0 :(得分:0)
您正在mFirebaseDatabase.getReference("list-abc05")
中检索不存在的节点。项目数据库的根目录未命名,因此:
mRef = mFirebaseDatabase.getReference();
此外,我建议仅在用户通过身份验证后附加侦听器(因此从onAuthStateChanged
内部)。这样你就已经知道了用户的UID,因此可以直接连接到他们的节点(而不是为所有用户检索数据):
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
authListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user == null) {
startActivity(new Intent(ViewInformation.this, LoginActivity.class));
finish();
}
else {
userID = user.getUid();
mRef.child(userID).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Toast.makeText(getApplicationContext(), "Showing Details...", Toast.LENGTH_SHORT).show();
showData(dataSnapshot);
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // never ignore errors
}
});
}
}
};
private void showData(DataSnapshot ds) {
userInformation uInfo = new userInformation();
uInfo.setName(ds.child(userID).child("Name").getValue(userInformation.class).getName());
uInfo.setGender(ds.child(userID).child("Gender").getValue(userInformation.class).getGender());
uInfo.setMobile_Number(ds.child(userID).child("Mobile_Number").getValue(userInformation.class).getMobile_Number());
uInfo.setAddress(ds.child(userID).child("Address").getValue(userInformation.class).getAddress());
Log.d(TAG, "showData: Name: " + uInfo.getName());
Log.d(TAG, "showData: Gender: " + uInfo.getGender());
Log.d(TAG, "showData: Mobile_Number: " + uInfo.getMobile_Number());
Log.d(TAG, "showData: Address: " + uInfo.getAddress());
ArrayList<String> array = new ArrayList<>();
array.add(uInfo.getName());
array.add(uInfo.getGender());
array.add(uInfo.getMobile_Number());
array.add(uInfo.getAddress());
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,array);
mListView.setAdapter(adapter);
}