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識別。任何其他提示將不勝感激!
注:我沒有刪除一些不相關的代碼,所以如果方括號不匹配或什麼是原因。
什麼不同班?把它放在哪裏? –
只需在usernameTextView.setText處添加id,您註釋掉了 –
usernameTextView.setText(dataSnapshot.getValue()。toString())的值是多少; 是Firebase的JSON嗎? –