Tal C
Tal C

Reputation: 567

How to input a string into a ViewHolder

I would like to put in a string from a different class into the following code:

 public class FirebaseCommentViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private View mView;
        private Context mContext;
        private String mphotoUserID;
        private String mUrl;


        public FirebaseCommentViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
            mContext = itemView.getContext();

            itemView.setOnClickListener(this);
        }

        public void bindComment(final Comment comment) {
            TextView usernameTextView = (TextView) mView.findViewById(R.id.comment_username);
            TextView comment_textview = (TextView) mView.findViewById(R.id.comment_textview);
            ImageButton moreOptionsImageButton = (ImageButton) mView.findViewById(R.id.comment_more_options);


            mphotoUserID = comment.getCommenter();
            mUrl = PhotoUtilities.removeWebPFromUrl(comment.getPhoto_url());

            //usernameTextView.setText(comment.getCommenter());
            setCommentorsName(comment.getCommenter(), usernameTextView);
            comment_textview.setText(comment.getCommentString());
}

        public void setCommentorsName(String uid, final TextView usernameTextView) {
            FirebaseDatabase.getInstance().getReference(FirebaseConstants.USERDATA).child(uid).child(FirebaseConstants.USERNAME)
                    .addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            if (dataSnapshot.getValue() != null) {
                                usernameTextView.setText(dataSnapshot.getValue().toString());

                            }
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {
                            usernameTextView.setText("BOB");
                        }
                    });
        }

        @Override
        public void onClick(View view) {
            final ArrayList<Comment> comments = new ArrayList<>();
    //      Reference correct section of database below
            Toast.makeText(mContext, "Item Clicked", Toast.LENGTH_SHORT).show();
            DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child(FirebaseConstants.PHOTOS)
                    .child(mUrl).child(FirebaseConstants.COMMENTS);
            ref.addListenerForSingleValueEvent(new ValueEventListener() {
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                        comments.add(snapshot.getValue(Comment.class));
                    }

    //                int itemPosition = getLayoutPosition();

    //                Intent intent = new Intent(mContext, RestaurantDetailActivity.class);
    //
    //                mContext.startActivity(intent);
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                }
            });

        }

The following is the code that I use in order to start the class:

private void setUpFirebaseAdapter() {
    mFirebaseAdapter = new FirebaseRecyclerAdapter<Comment, FirebaseCommentViewHolder>
            (Comment.class, R.layout.comment_template, FirebaseCommentViewHolder.class,
                    mCommentReference) {

        @Override
        protected void populateViewHolder(FirebaseCommentViewHolder viewHolder,
                                          Comment model, int position) {
            viewHolder.bindComment(model);
        }
    };
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    mRecyclerView.setAdapter(mFirebaseAdapter);
    mRecyclerView.setVisibility(View.VISIBLE);
}

Basically I need to pass in the user ID from the code into the FirebaseCommentViewHolder class and I am unsure how as I have never implemented code like this before. I tried to add it to the constructor, but this did not work/get recognized by Android Studio. Any other tips would be greatly appreciated!

NOTE: I did delete some non relevant code so if brackets don't match up or something that would be why.

Upvotes: 0

Views: 113

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 191854

Basically I need to pass in the user ID from the code into the FirebaseCommentViewHolder class

I would just add a parameter

    @Override
    protected void populateViewHolder(FirebaseCommentViewHolder viewHolder,
             Comment model, int position) {
        // Grab your userId from somewhere
        viewHolder.bindComment(model, userId);
    }

You can't add to the constructor of the ViewHolder because FirebaseUI is abstracting that away from your code.

You could alternatively extend the FirebaseRecyclerAdapter and add it into that constructor

private void setUpFirebaseAdapter(final String userId) {
    // For example
    mFirebaseAdapter = new UserIdFirebaseRecyclerAdapter<Comment, FirebaseCommentViewHolder>
            (userId, Comment.class, R.layout.comment_template, FirebaseCommentViewHolder.class,
                    mCommentReference) {

Either way, you would get it into the ViewHolder via the bindComment() method.

Upvotes: 1

Related Questions