Reputation: 658
I am trying to make a new user in Firebase
having 4 fields
1.username
2.posts
3.followers
4.following --> ArrayList
where following is an ArrayList
but the last field doesn't get created in Firebase at all.
Here is the result i am getting:
Code for creating new user:
UserBean userBean = new UserBean(mUsername.toLowerCase(),0,0,new ArrayList<String>());
mUserDatabase.push().setValue(userBean);
My UserBean:
String username;
int followers,post;
ArrayList<String> following;
public UserBean() {}
public UserBean(String username, int followers, int post, ArrayList<String> following) {
this.username = username;
this.followers = followers;
this.post = post;
this.following = following;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getFollowers() {
return followers;
}
public void setFollowers(int followers) {
this.followers = followers;
}
public int getPost() {
return post;
}
public void setPost(int post) {
this.post = post;
}
public ArrayList<String> getFollowing() {
return following;
}
public void setFollowing(ArrayList<String> following) {
this.following = following;
}
I also tried adding the data to the ArrayList
anyways but the data does not get added in the Firebase
. Here is what i have tried:
Query currentUserQuery = mUsersReference.orderByChild("username").equalTo(mFirebaseAuth.getCurrentUser().getDisplayName().toLowerCase());
Log.d(MainActivity.TAG,mFirebaseAuth.getCurrentUser().getDisplayName().toLowerCase());
currentUserQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot data: dataSnapshot.getChildren()){
ArrayList<String> following = (ArrayList<String>) data.child("following").getValue();
following.add(username);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
ArrayList<String> following = (ArrayList<String>) data.child("following").getValue();
This line of code throws NullPointerException
but it should have added the data in the list as you guys are saying the list just doesn't show up without any data. Then why does it throw NullPointerException
?
Upvotes: 3
Views: 1140
Reputation: 658
So Apparently, @Vinay Avasthi is right, Firebase doesn't show an empty ArrayList
in the database so if you are initializing your object and passing an empty ArrayList
in your constructor then the ArrayList
won't show up in Firebase
which further gives you one more problem that; In future, if you want to add the data in the list, you cannot do so as it gives you the NullPointerException
so to tackle all these problem what i had to do is pass an ArrayList
with an empty String
then i could show that List
in the firebase
and refer it easily.
ArrayList<String> following = new ArrayList<>();
following.add("");
UserBean userBean = new UserBean(mUsername.toLowerCase(),0,0,following);
mUserDatabase.push().setValue(userBean);
One more important thing to note , also pointed by @Vinay Avasthi is that you also have to set the value of the list again to your new list otherwise you would be adding the data in the list but the data won't update in the Firebase
.
ArrayList<String> following = (ArrayList<String>) data.child("following").getValue();
following.add(username);
String key = data.getKey();
mUsersReference.child(key).child("following").setValue(following);
But there is still one issue though that i am facing, that in every list you will be having an empty String
for which you have no use.
For now, i will just ignore the empty String
while showing users their followings
. But I think firebase
should have created the empty ArrayList
.
Upvotes: 0
Reputation: 952
As I understand, firebase does not create array items if there are no elements in it. If you add an element in the list, you will see it in firebase.
Upvotes: 1