0

Je voudrais mettre dans une chaîne à partir d'une autre classe dans le code suivant:Comment entrer une chaîne dans un 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) { 
       } 
      }); 

     } 

Voici le code que j'utilise pour commencer la classe:

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

Fondamentalement, je besoin de passer dans l'ID utilisateur du code dans la classe FirebaseCommentViewHolder et je ne suis pas sûr comment comme je l'ai jamais mis en œuvre le code comme ça avant. J'ai essayé de l'ajouter au constructeur, mais cela n'a pas fonctionné/a été reconnu par Android Studio. Tous les autres conseils seraient grandement appréciés!

REMARQUE: J'ai supprimé un code non pertinent, donc si les crochets ne correspondent pas ou quelque chose qui serait pourquoi.

+0

Quelle classe différente? Mettez-le où? –

+0

Il suffit de mettre l'ID à 'usernameTextView.setText', que vous avez commenté –

+0

quelle est la valeur de usernameTextView.setText (dataSnapshot.getValue(). ToString()); est-ce le JSON de Firebase? –

Répondre

1

Fondamentalement, je besoin de passer dans l'ID utilisateur du code dans la classe FirebaseCommentViewHolder

Je voudrais juste ajouter un paramètre

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

Vous ne pouvez pas ajouter au constructeur de le ViewHolder parce que FirebaseUI l'extrait de votre code.

Vous pourriez aussi étendre le FirebaseRecyclerAdapter et l'ajouter dans que constructeur

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

De toute façon, vous obtiendrez dans le ViewHolder via la méthode bindComment().