0

我想提出一個字符串來自不同班級分成以下代碼:如何輸入字符串轉換成ViewHolder

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識別。任何其他提示將不勝感激!

注:我沒有刪除一些不相關的代碼,所以如果方括號不匹配或什麼是原因。

+0

什麼不同班?把它放在哪裏? –

+0

只需在usernameTextView.setText處添加id,您註釋掉了 –

+0

usernameTextView.setText(dataSnapshot.getValue()。toString())的值是多少; 是Firebase的JSON嗎? –

回答

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

無論哪種方式,你會得到它進入ViewHolder通過bindComment()方法。