我的3 long
正在阅读,但我的TextView String
(s)正在返回null。
这是添加新用户数据的代码:
public void addNewUser(String email, String address, String username, String interests, String website, String bio, String profile_photo){
User user = new User(userID, address, 1, email, StringManipulation.condenseUsername(username));
myRef.child(mContext.getString(R.string.dbname_users))
.child(userID)
.setValue(user);
UserAccountSettings settings = new UserAccountSettings(
bio,
username,
0,
interests,
profile_photo,
0,
0,
StringManipulation.condenseUsername(username),
website
);
myRef.child(mContext.getString(R.string.dbname_users_account_settings))
.child(userID)
.setValue(settings);
}
这是从Firebase数据库中检索用户数据的代码:
public UserSettings getUserSettings(DataSnapshot dataSnapshot){
Log.d(TAG, "getUserAccountSettings: retrieving user_accounter_settings from firebase.");
UserAccountSettings settings = new UserAccountSettings();
User user = new User();
for (DataSnapshot ds: dataSnapshot.getChildren()){
//users node
if (ds.getKey().equals(mContext.getString(R.string.dbname_users))){
Log.d(TAG, "getUserAccountSettings: datasnapshot: " + ds);
try{
user.setUsername(
ds.child(userID)
.getValue(User.class)
.getUsername()
);
user.setEmail(
ds.child(userID)
.getValue(User.class)
.getEmail()
);
user.setPhone_number(
ds.child(userID)
.getValue(User.class)
.getPhone_number()
);
user.setUser_id(
ds.child(userID)
.getValue(User.class)
.getUser_id()
);
user.setAddress(
ds.child(userID)
.getValue(User.class)
.getAddress()
);
Log.d(TAG, "getUserAccountSettings: users information: " + user.toString());
}catch (NullPointerException e){
Log.e(TAG, "getUserAccountSettings: NullPointerException: " + e.getMessage() );
}
//user_account_settings node
if (ds.getKey().equals(mContext.getString(R.string.dbname_users_account_settings))) {
Log.d(TAG, "getUserAccountSettings: datasnapshot: " + ds);
settings.setDisplay_name(
ds.child(userID)
.getValue(UserAccountSettings.class)
.getDisplay_name()
);
settings.setUsername(
ds.child(userID)
.getValue(UserAccountSettings.class)
.getUsername()
);
settings.setWebsite(
ds.child(userID)
.getValue(UserAccountSettings.class)
.getWebsite()
);
settings.setInterests(
ds.child(userID)
.getValue(UserAccountSettings.class)
.getInterests()
);
settings.setProfile_photo(
ds.child(userID)
.getValue(UserAccountSettings.class)
.getProfile_photo()
);
settings.setBio(
ds.child(userID)
.getValue(UserAccountSettings.class)
.getBio()
);
settings.setTrustworthy(
ds.child(userID)
.getValue(UserAccountSettings.class)
.getTrustworthy()
);
settings.setTruefans(
ds.child(userID)
.getValue(UserAccountSettings.class)
.getTruefans()
);
settings.setFollowers(
ds.child(userID)
.getValue(UserAccountSettings.class)
.getFollowers()
);
Log.d(TAG, "getUserAccountSettings: retrieved user_account_settings information: " + settings.toString());
}
}
}
return new UserSettings(user, settings);
}
这是UserAccountSettings类的代码:
public class UserAccountSettings {
private String bio;
private String display_name;
private long followers;
private String interests;
private String profile_photo;
private long truefans;
private long trustworthy;
private String username;
private String website;
public UserAccountSettings(String bio, String display_name, long followers, String interests, String profile_photo, long truefans, long trustworthy, String username, String website) {
this.bio = bio;
this.display_name = display_name;
this.followers = followers;
this.interests = interests;
this.profile_photo = profile_photo;
this.truefans = truefans;
this.trustworthy = trustworthy;
this.username = username;
this.website = website;
}
public UserAccountSettings() {
}
public String getBio() {
return bio;
}
public void setBio(String bio) {
this.bio = bio;
}
public String getDisplay_name() {
return display_name;
}
public void setDisplay_name(String display_name) {
this.display_name = display_name;
}
public long getFollowers() {
return followers;
}
public void setFollowers(long followers) {
this.followers = followers;
}
public String getInterests() {
return interests;
}
public void setInterests(String interests) {
this.interests = interests;
}
public String getProfile_photo() {
return profile_photo;
}
public void setProfile_photo(String profile_photo) {
this.profile_photo = profile_photo;
}
public long getTruefans() {
return truefans;
}
public void setTruefans(long truefans) {
this.truefans = truefans;
}
public long getTrustworthy() {
return trustworthy;
}
public void setTrustworthy(long trustworthy) {
this.trustworthy = trustworthy;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website = website;
}
@Override
public String toString() {
return "UserAccountSettings{" +
"bio='" + bio + '\'' +
", display_name='" + display_name + '\'' +
", followers=" + followers +
", interests='" + interests + '\'' +
", profile_photo='" + profile_photo + '\'' +
", truefans=" + truefans +
", trustworthy=" + trustworthy +
", username='" + username + '\'' +
", website='" + website + '\'' +
'}';
}
这是setProfileWidgets
方法的代码:
private void setProfileWidgets(UserSettings userSettings){
Log.d(TAG, "setProfileWidgets: settings widgets with data retrieving from firebase database: " + userSettings.toString());
//User user = userSettings.getUser();
UserAccountSettings settings = userSettings.getSettings();
//UniversalImageLoader.setImage(settings.getProfile_photo(), mProfilePhoto, null , "");
mDisplayname.setText(String.valueOf(settings.getDisplay_name()));
mUsername.setText(String.valueOf(settings.getUsername()));
mBio.setText(String.valueOf(settings.getBio()));
mIntersts.setText(String.valueOf(settings.getInterests()));
mWebsite.setText(String.valueOf(settings.getWebsite()));
mTruefans.setText(String.valueOf(settings.getTruefans()));
mTrustworthy.setText(String.valueOf(settings.getTrustworthy()));
mFollowers.setText(String.valueOf(settings.getFollowers()));
}
我的addValueEventListener
方法:
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
//retrieve user inforation from the database
setProfileWidgets(mFirebaseMethod.getUserSettings(dataSnapshot));
//retrieve images for the user in question
}
这是我在Firebase数据库中的数据结构的图像:
这是我的Android模拟器中结果为null
的图片:它假设显示用户名。
谢谢!
在顶部,我没有应用Asynchronous Firebase API。下面,我刚刚添加了它。如果你有点困惑,请告诉我。
这是应用Asynchronous Firebase API时的代码:
private void readData(final FirebaseCallback firebaseCallback) {
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
setProfileWidgets(mFirebaseMethod.getUserSettings(dataSnapshot));
firebaseCallback.onCallback();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
};
myRef.addListenerForSingleValueEvent(valueEventListener);
}
private interface FirebaseCallback{
void onCallback();
}
以下是onCreateView
的{{1}}方法:
readData
我有什么遗漏的吗?让我知道,谢谢!