如何将字符串输入ViewHolder

时间:2017-09-04 21:58:52

标签: android firebase firebase-realtime-database android-recyclerview firebaseui

我想将来自不同类的字符串放入以下代码中:

 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) {
                }
            });

        }

以下是我用来启动课程的代码:

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);
}

基本上我需要将代码中的用户ID传递到FirebaseCommentViewHolder类中,我不确定如何从未实现过这样的代码。我试图将它添加到构造函数中,但这不起作用/得到Android Studio的识别。任何其他提示将不胜感激!

注意:我确实删除了一些不相关的代码,因此如果括号不匹配或者是什么原因。

1 个答案:

答案 0 :(得分:1)

  

基本上我需要将代码中的用户ID传入FirebaseCommentViewHolder类

我只想添加一个参数

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

您无法添加到ViewHolder的构造函数,因为FirebaseUI正在从代码中抽象出来。

您也可以扩展FirebaseRecyclerAdapter并将其添加到那个构造函数

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

无论哪种方式,您都可以通过bindComment()方法将其放入ViewHolder。