My 3 long
is reading but my TextView String
(s) are returning as null.
This is the code for adding a new user data:
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);
}
This is the code for retrieving User's data from Firebase Database:
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);
}
This is the code for UserAccountSettings class:
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 + '\'' +
'}';
}
This is the code for setProfileWidgets
method:
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()));
}
My addValueEventListener
method:
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
}
This is the image for my Data Structure in Firebase Database:
This is the image for resulting as null
in my Android Emulator: And it suppose to be display the username.
Thank you!
In the top, I did not apply the Asynchronous Firebase API. Below, I just added it. If you are little confuse, let me know.
This is the code when applying the 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();
}
Here is for the onCreateView
method for readData
:
readData(new FirebaseCallback() {
@Override
public void onCallback() {
Log.d(TAG, "onCallback: ");
}
});
Is there anything I missing? Let me know, thank you!