我想让这段代码做的是:每当用户点击这个项目(例如:喜欢或不喜欢),我希望我的firebase发生一些事情(例如,设置like的值为1)。我正努力为每一个项目(比如,不喜欢,快乐表情,报告)设置一个点击监听器。即使我在静态类中设置了click侦听器,我也不能调用我的数据库引用。我也试过做commentsactivity.this.mreviewsdatabase..等等,但它不工作,因为它不需要静态类。如果我从类中删除了static,应用程序就会崩溃。
public class CommentsActivity extends AppCompatActivity {
private RecyclerView mCommentList;
public DatabaseReference mReviewsDatabase;
private FirebaseUser mCurrentUser;
private ImageView happyEmote, thumpUp, thumbDown ,reportReview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comments);
String title = getIntent().getStringExtra("EXTRA_SESSION_ID");
mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
mReviewsDatabase = FirebaseDatabase.getInstance().getReference().child("Film_reviews").child(title);
mCommentList = (RecyclerView)findViewById(R.id.reviews_list);
mCommentList.setHasFixedSize(true);
mCommentList.setLayoutManager(new LinearLayoutManager(this));
FirebaseRecyclerAdapter<AllCommClass, CommentsActivity.ReviewsViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<AllCommClass, CommentsActivity.ReviewsViewHolder>(
AllCommClass.class,
R.layout.comment_single_layout,
CommentsActivity.ReviewsViewHolder.class,
mReviewsDatabase
) {
@Override
protected void populateViewHolder(CommentsActivity.ReviewsViewHolder reviewsViewHolder, AllCommClass allCommClass, int i) {
reviewsViewHolder.setUsername(allCommClass.getUsername());
reviewsViewHolder.setReview(allCommClass.getReview());
reviewsViewHolder.setVoto(allCommClass.getVoto());
reviewsViewHolder.setReport();
reviewsViewHolder.setLike();
reviewsViewHolder.setDislike();
reviewsViewHolder.setHappyEmote();
reviewsViewHolder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { }
});
}
};
mCommentList.setAdapter(firebaseRecyclerAdapter);
}
public static class ReviewsViewHolder extends RecyclerView.ViewHolder {
View mView;
public ReviewsViewHolder(@NonNull View itemView) {
super(itemView);
mView = itemView;
}
public void setReport() {
ImageView reportReview = mView.findViewById(R.id.reportReview);
reportReview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.v("Tag","You've reported the comment number "+getAdapterPosition());
}
});
}
public void setLike() {
ImageView thumpUp = mView.findViewById(R.id.thumbUp);
thumpUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.v("Tag","You've liked the comment number "+getAdapterPosition());
}
});
}
public void setDislike() {
ImageView thumpDown = mView.findViewById(R.id.thumbDown);
thumpDown.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.v("Tag","You've disliked the comment number "+getAdapterPosition());
}
});
}
public void setHappyEmote() {
ImageView happyEmote = mView.findViewById(R.id.happyEmote);
happyEmote.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.v("Tag","You've added a reaction to the comment number "+getAdapterPosition());
}
});
}
public void setUsername(String username){
TextView titleView = mView.findViewById(R.id.user_single_username_comment_section);
titleView.setText(username);
}
public void setReview(String review){
TextView titleView = mView.findViewById(R.id.user_single_review);
titleView.setText(review);
}
public void setVoto(int voto){
TextView titleView = mView.findViewById(R.id.user_single_rating);
titleView.setText(String.valueOf(voto));
}
}}
"
2条答案
按热度按时间crcmnpdw1#
对于初学者来说,基本上有两种方法可以将click listener设置为recycler视图项。
首先是简单但非最佳的方法。在适配器类的onbindviewholder中,将onclicklistener设置为holder.itemview
第二种方法是使用onclick接口,然后从calling/main活动调用它。如果你需要进一步的指导,请告诉我
z2acfund2#
当我们从firebase获取数据时
这个过程是匹配firebase上的项的id,您通过模型类获取它来填充您的recycleview。
如果希望应用程序记住某个按钮上的“喜欢”或“不喜欢”的数目,则需要根据用户将执行单击的项目的id设置计数。
您可以显示该计数或以这种方式跟踪按钮的单击。
如果你需要密码,请告诉我