所以我试图从Firebase检索数据,但是问题是当我将User类传递给方法时,在所有参数上都得到空值,这是方法:
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Log.e(TAG, "onDataChange: Retrieving data.....");
FirebaseMethods firebaseMethods = new FirebaseMethods(mContext);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
public User getUserSettings(DataSnapshot dataSnapshot){
Log.e(TAG, "getUserSettings: Retrieving Data......");
User user = new User();
for (DataSnapshot ds: dataSnapshot.getChildren()){
if(ds.hasChild(userID)){
user.setDescription(ds.child(userID).getValue(User.class).getDescription());
user.setDisplay_name(ds.child(userID).getValue(User.class).getDisplay_name());
user.setEmail(ds.child(userID).getValue(User.class).getEmail());
user.setFollowers(ds.child(userID).getValue(User.class).getFollowers());
user.setFollowing(ds.child(userID).getValue(User.class).getFollowing());
user.setPosts(ds.child(userID).getValue(User.class).getPosts());
user.setProfile_photo(ds.child(userID).getValue(User.class).getProfile_photo());
user.setUser_id(ds.child(userID).getValue(User.class).getUser_id());
user.setUsername(ds.child(userID).getValue(User.class).getUsername());
}
}
Log.e(TAG, "getUserSettings: User file"+user.toString());
return new User();
}
直到这里Log.e(TAG, "getUserSettings: User file"+user.toString());
给出正确的输出User fileUser{description='None', display_name='kadiem', email='kazm98@gmail.com', followers='0', following='0', posts='0', profile_photo='None', user_id='L8qHpeb3LpfrzCOHdLVWcEK6XDv2', username='kadiem'}
那我有这个方法
private void setupWidgets(User user) {
Log.d(TAG, "setupWidgets: user data received "+user.toString());
mDescription.setText(user.getDescription());
mDisplayname.setText(user.getDisplay_name());
mUsername.setText(user.getUsername());
mFollowing.setText(user.getFollowing());
mFollowers.setText(user.getFollowers());
mPosts.setText(user.getPosts());
}
当我将setupWidgets(firebaseMethods.getUserSettings(dataSnapshot));
传递到databaseReference
时出现了问题,我从setupWidgets(User user)
方法获得的日志包含空参数