Home > OS >  FirebaseRecyclerAdapter model returning null values
FirebaseRecyclerAdapter model returning null values

Time:10-29

I'm trying to make a comment system for posts on my social media app. In my database each post has a section inside of "comments" table, like so:

Firebase Comment Table

"hypno--######" is the title of the social media post. It Contains the comment, user id of the user who posted the comment, and a unixtimestamp when the comment was posted. Each comment is titled after the time it was posted.

This is the Comment class

public class comment {

    public String uID;
    public String comment_t;
    public long unixTimestamp;

    public comment() {
        // Default constructor required for calls to DataSnapshot.getValue(User.class)
    }
    public comment(String uID, String comment_t, long unixTimestamp) {
        this.uID = uID;
        this.comment_t = comment_t;
        this.unixTimestamp = unixTimestamp;


    }

    public String getuID() {
        return uID;
    }

    public void setuID(String uID) {
        this.uID = uID;
    }

    public String getComment() {return comment_t;}

    public void setComment() {this.comment_t = comment_t; }

    public long getUnixTimestamp() {
        return unixTimestamp;
    }
}

This is the Comment Adapter:

Public class Adapter_Comment extends FirebaseRecyclerAdapter<comment, Adapter_Comment.ViewHolder_com> {

    private DatabaseReference mDatabase;
    private static final String TAG = "RecyclerViewAdapter";
    private Context mContext;
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

    private static AppCompatActivity unwrap(Context context) {
        while (!(context instanceof Activity) && context instanceof ContextWrapper) {
            context = ((ContextWrapper) context).getBaseContext();
        }

        return (AppCompatActivity) context;
    }

    public Adapter_Comment(@NonNull FirebaseRecyclerOptions<comment> options) {
        super(options);
        //this.mContext = mContext;
    }

    @NonNull
    @Override
    public ViewHolder_com onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_comment, parent, false);
        mDatabase = FirebaseDatabase.getInstance().getReference();

        return new ViewHolder_com(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder_com holder, int position, @NonNull comment model) {
        mDatabase = FirebaseDatabase.getInstance().getReference();
        
        long dv = model.getUnixTimestamp()*-1000;
        Date df = new java.util.Date(dv);
        String vv = new SimpleDateFormat("MM dd, yyyy hh:mma", Locale.ENGLISH).format(df);

        holder.time.setText(vv);
        String com = model.getComment();
        holder.comment_text.setText(com);

        mDatabase.child("users").child(model.getuID()).child("profileUrl").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                if (snapshot.exists())
                {
                    final String picUrl = snapshot.getValue(String.class);
                    Glide.with(holder.postPfp.getContext()).load(picUrl).into(holder.postPfp);
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) { }
        });

        holder.postPfp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //AppCompatActivity activity = (AppCompatActivity) v.getContext();
                AppCompatActivity activity = unwrap(v.getContext());
                Fragment OtherProfileFragment = new OtherProfileFragment();

                Bundle bundle = new Bundle();
                bundle.putString("key", model.getuID());
                OtherProfileFragment.setArguments(bundle);
                activity.getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, OtherProfileFragment).addToBackStack(null).commit();
            }
        });

    }


    public class ViewHolder_com extends RecyclerView.ViewHolder {

        TextView comment_text;
        CircleImageView postPfp;
        TextView time;
        RelativeLayout comment_layout;


        public ViewHolder_com(@NonNull View itemView) {
            super(itemView);

            postPfp = itemView.findViewById(R.id.iv_comment_icon);
            comment_text = itemView.findViewById(R.id.tv_comment_text);
            time = itemView.findViewById(R.id.tv_comment_time);
            comment_layout = itemView.findViewById(R.id.comment_layout);


        }
    }
}

This is Comment Fragment:

public class CommentFragment  extends Fragment {

    private DatabaseReference mDatabase;
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    View view;
    String value;
    RecyclerView recyclerView;
    Query query;
    TextView comment_text;
    long unixTime = System.currentTimeMillis() / 1000L;
    public long globalUnix;
    Button comment_post;
    String comment_string;
    Adapter_Comment adapter;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_comment, container, false);
        value = getArguments().getString("key");
        mDatabase = FirebaseDatabase.getInstance().getReference();
        recyclerView = view.findViewById(R.id.recyclerv_comment);
        comment_text = view.findViewById(R.id.tv_comment_type);
        comment_post = view.findViewById(R.id.btn_comment_post);
        globalUnix = (unixTime * -1);

        comment_post.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(comment_text.getText().toString() == NULL){
                    Toast.makeText(getActivity(), "No Comment Typed", Toast.LENGTH_LONG).show();
                }
                else{
                    
                    comment com = new comment();
                    
                    com.uID = user.getUid();
                    com.comment_t = comment_text.getText().toString();
                    com.unixTimestamp = globalUnix;

                    mDatabase.child("comments").child(value).child(globalUnix   "").setValue(com);

                }
            }
        });

        initRecyclerView();
        return view;
    }

    private void initRecyclerView(){
        //Log.d(TAG, "initRecyclerView: init recyclerView");

        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(layoutManager);

        query = FirebaseDatabase.getInstance().getReference().child("comments").orderByValue();

        FirebaseRecyclerOptions<comment> options = new FirebaseRecyclerOptions.Builder<comment>().setQuery(query, comment.class).build();

        adapter = new Adapter_Comment(options);
        recyclerView.setAdapter(adapter);
        adapter.startListening();
        adapter.notifyDataSetChanged();

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }
}

Inside of the adapter I'm using the comment model, to get the uID, comment and timestamp to fill the holder, however when i set these values im getting null values. Is there something im missing when trying to connect the adapter/firebase and model/holder?

long dv = model.getUnixTimestamp()*-1000;
    Date df = new java.util.Date(dv);
    String vv = new SimpleDateFormat("MM dd, yyyy hh:mma", Locale.ENGLISH).format(df);

holder.time.setText(vv);
String com = model.getComment();
holder.comment_text.setText(com);

mDatabase.child("users").child(model.getuID()).child("profileUrl").addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        if (snapshot.exists())
        {
            final String picUrl = snapshot.getValue(String.class);
            Glide.with(holder.postPfp.getContext()).load(picUrl).into(holder.postPfp);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) { }
});

CodePudding user response:

There's really too much going on in here, but...

As far as I can see you're creating a FirebaseUI adapter on FirebaseDatabase.getInstance().getReference().child("comments"). FirebaseUI adapters show the direct child nodes of the node you pass in, so in your case it'll create one view for the hypno---...196 node. You're trying to read a Comment object from there, but don't exist until one level lower in your JSON.

So you can:

  • Either show the comments for one post, by basing the adapter off of that. So: FirebaseDatabase.getInstance().getReference().child("comments").child("hypno---...196") (which the real key in there).
  • Or you can show one piece of information about each post, for example its key.

If you want to show a flat list of comments for all posts through the FirebaseUI adapter, you'll have to store a flat list of comments across all posts in your database too.

  • Related