Search code examples
androidfirebasefirebase-realtime-databaseandroid-recyclerviewfirebaseui

Getting some null values with firebase and other values normal


So i'm using Firebase Database to post and retrieve some data.

Posting data works fine, and here is how my tree looks like for the posts:

Tree

Here is my posting method ( works fine )

public void post_status(){
    String full_song = song;
    final String song_name = full_song.split("//")[0];
    final String song_Artist = full_song.split("//")[1];

    final DatabaseReference db_post = DB_NewPost.push();
    if (song_name != null && song_Artist !=null && user_name != null && profile_image != null) {
        final ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
        progressDialog.setTitle("Posting...");
        progressDialog.show();
        DB_NewPost.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                db_post.child("artist").setValue(song_Artist);
                db_post.child("song_name").setValue(song_name);
                db_post.child("profile_image").setValue(profile_image);
                db_post.child("user_name").setValue(user_name);
                db_post.child("user_comment").setValue("Test Comment");
                db_post.child("user_ID").setValue(user.getUid()).addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        progressDialog.dismiss();
                    }
                });

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                progressDialog.dismiss();
                CafeBar.builder(MainActivity.this)
                        .duration(CafeBar.Duration.MEDIUM)
                        .content("Post " + databaseError.getMessage())
                        .maxLines(4)
                        .theme(CafeBarTheme.Custom(Color.parseColor("#E65100")))
                        .show();
            }
        });
    }
    else {
        CafeBar.builder(MainActivity.this)
                .duration(CafeBar.Duration.MEDIUM)
                .content("Post canceled, make sure all the values are set")
                .maxLines(4)
                .theme(CafeBarTheme.Custom(Color.parseColor("#E65100")))
                .show();
    }
}

Here is my RecylcerView related code:

@Override
public void onStart() {
    super.onStart();
    Query query = DB_NewPost.limitToLast(10);
    FirebaseRecyclerOptions<user_post> options =
            new FirebaseRecyclerOptions.Builder<user_post>()
                    .setQuery(query, user_post.class)
                    .build();
    FBRA = new FirebaseRecyclerAdapter<user_post, postViewHolder>(options) {
        @Override
        public postViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.post_layout, parent, false);

            return new postViewHolder(view);
        }

        @Override
        protected void onBindViewHolder(@NonNull postViewHolder holder, int position, @NonNull user_post model) {

            holder.setUserName(model.getUsername());
            holder.setComment(model.getUser_comment());
            holder.setSongArtist("by: " + model.getSong_artist());
            holder.setSongName(model.getSong_name());
            holder.setPicture(getApplicationContext(), model.getImage());
            Log.e("UserName", "" + model.getUsername());
            Log.e("Comment", "" +model.getUser_comment());
            Log.e("Artist", "" +model.getSong_artist());
            Log.e("SongName", "" +model.getSong_name());
            Log.e("Picture", "" +model.getImage());

        }
    };
    mSongPost.setAdapter(FBRA);
    FBRA.startListening();

}

public static class postViewHolder extends RecyclerView.ViewHolder{

    View mView;
    public postViewHolder(View itemView) {
        super(itemView);
        mView = itemView;
    }

    public void setSongName(String songName){
        TextView song_name = mView.findViewById(R.id.song_name);
        song_name.setText(songName);
    }

    public void setSongArtist(String Artist){
        TextView song_artist = mView.findViewById(R.id.song_artist);
        song_artist.setText(Artist);
    }

    public void setUserName(String userName){
        TextView user_name = mView.findViewById(R.id.user_name);
        user_name.setText(userName);
    }

    public void setComment(String comment){
        TextView user_comment = mView.findViewById(R.id.user_comment_status);
        user_comment.setText(comment);
    }

    public void setPicture(Context ctx,String image){
        CircleImageView profile = mView.findViewById(R.id.photo_view);
       RequestOptions options = new RequestOptions();
        options.centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .fitCenter()
                .circleCrop();
        Glide.with(ctx)
                .load(image)
                .apply(options)
                .into(profile);
    }

}

And here is where i am facing issues, i'm getting the String value of SongName and the comment ( even it it's fixed ), but NULL values for everything else. I tried almost everything, searched alot of forums and previous questions regarding seperating data and so.. i couldn't find any solution. Anyone care to help me with this ?

Here is my log:

01-30 16:29:52.736 25015-25015/xerxes.music_feed E/UserName: null 01-30 16:29:52.737 25015-25015/xerxes.music_feed E/Comment: Test Comment 01-30 16:29:52.737 25015-25015/xerxes.music_feed E/Artist: null 01-30 16:29:52.737 25015-25015/xerxes.music_feed E/SongName: The One Hundred - Monster 01-30 16:29:52.737 25015-25015/xerxes.music_feed E/Picture: null

EDIT: I also noticed this in my log, although i have setters and getters for those variabls.

01-30 16:29:52.734 25015-25015/xerxes.music_feed W/ClassMapper: No setter/field for profile_image found on class xerxes.music_feed.user_post 01-30 16:29:52.734 25015-25015/xerxes.music_feed W/ClassMapper: No setter/field for artist found on class xerxes.music_feed.user_post 01-30 16:29:52.734 25015-25015/xerxes.music_feed W/ClassMapper: No setter/field for user_ID found on class xerxes.music_feed.user_post 01-30 16:29:52.734 25015-25015/xerxes.music_feed W/ClassMapper: No setter/field for user_name found on class xerxes.music_feed.user_post

EDIT 2: Added Setters/Getters class

public class user_post {
private String username;
private String song_name;
private String song_artist;
private String user_comment;
private String image;

public user_post(){

}

public user_post(String username, String song_name, String song_artist, String user_comment,String p_image){
    this.username = username;
    this.song_name = song_name;
    this.song_artist = song_artist;
    this.user_comment = user_comment;
    this.image = p_image;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getSong_name() {
    return song_name;
}

public void setSong_name(String song_name) {
    this.song_name = song_name;
}

public String getSong_artist() {
    return song_artist;
}

public void setSong_artist(String song_artist) {
    this.song_artist = song_artist;
}

public String getUser_comment() {
    return user_comment;
}

public void setUser_comment(String user_comment) {
    this.user_comment = user_comment;
}

public String getImage() {
    return image;
}

public void setUserImage(String image) {
    this.image = image;
}

}


Solution

  • in your POJO refactor this line

    private String username;
    

    to this

    private String user_name;
    

    Explanation to people that maybe is having the same issue

    When you do your POJO class with all the setters and getters , all the Strings or ints , longs , etc SHOULD match the same names as your database structure, and check if the data from the database has "" , for example phone_number:"359182" , if your datbaase is like that is because you need to declare in your POJO String phone_number and NOT long or int phone_number... if in your database the value of phone_number is phone_number: 3049512 without the "" it means that the value is either an int , double or long

    happy coding