我尝试从firebase数据库中的回收器视图设置upvote的值,即当用户单击“喜欢”按钮时,upvote的值将递增1,但如果用户再次单击“喜欢”按钮,upvote的值将递减1。为此,我尝试切换和if-else,但由于它保留在set Value()部分中,因此不起作用。
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {
public static Context context;
List<String[]> myDataList;
List<qna_data> mydataList;
int values ;
Long number;
private Long likeCount;
private Long downlikeCount;
private boolean isLiked = false;
private DatabaseReference qnaRef;
List<DataSnapshot> dataSnapshotList;
public MyRecyclerViewAdapter(List<qna_data> myDataList) {
this.mydataList = myDataList;
}
public MyRecyclerViewAdapter(DatabaseReference qnaRef, List<DataSnapshot> dataSnapshotList) {
this.qnaRef = qnaRef;
this.dataSnapshotList = dataSnapshotList;
}
// Create new views (invoked by the layout manager)
@Override
public MyRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycleview_post_profile, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(MyRecyclerViewAdapter.ViewHolder holder, int position) {
qna_data data = mydataList.get(position);
context = holder.itemView.getContext();
holder.question_asked.setText(data.getQuestion_qna_asked());
holder.time_of_question.setText(data.getQna_timestamp_asked());
holder.category_choosen.setText(data.getCategory_qna_asked());
holder.username.setText(data.getUser_qna_name_asked());
holder.upvote.setText(String.valueOf(data.getUpvote()));
holder.downvote.setText(String.valueOf(data.getDownvote()));
holder.upvote_liner.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
number = data.getUpvote();
String pushkey = data.getPushkey();
String scholarid = data.getScholar_id();
likeCount = number + 1;
DatabaseReference answersRef = FirebaseDatabase.getInstance().getReference("QnA/Questions").child(pushkey).child(scholarid);
switch (values) {
case 0:
System.out.println(values);
System.out.println("it is liked");
answersRef.child("upvote").setValue(likeCount).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast.makeText(view.getContext(), "Toast", Toast.LENGTH_SHORT).show();
}
});
values = 1;
System.out.println(values);
break;
case 1:
System.out.println("");
Toast.makeText(view.getContext(), "clicked", Toast.LENGTH_SHORT).show();
values = 0;
break;
default:
System.out.println("unknown value of isLiked");
break;
}
// if (isLiked == false) {
// System.out.println("isLiked is false");
// isLiked = true;
// System.out.println("isLiked is now true");
// answersRef.child("upvote").setValue(likeCount);
//
// return;
// } else {
// System.out.println("isLiked is true");
// Toast.makeText(view.getContext(), "Toast", Toast.LENGTH_SHORT).show();
// }
}
});
Glide.with(holder.profile_of_users.getContext())
.load(data.getImage()).into(holder.profile_of_users);
}
@Override
public int getItemCount() {
return mydataList.size();
}
// ViewHolder class
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView question_asked, time_of_question, category_choosen, username, upvote, downvote;
de.hdodenhof.circleimageview.CircleImageView profile_of_users;
LinearLayoutCompat linear, upvote_liner;
AppCompatImageView upimage;
public ViewHolder(View itemView) {
super(itemView);
linear = itemView.findViewById(R.id.answer_question_qna);
profile_of_users = itemView.findViewById(R.id.profile_qna);
profile_of_users = itemView.findViewById(R.id.profile_qna);
username = itemView.findViewById(R.id.user_name_qna);
time_of_question = itemView.findViewById(R.id.timestamp_qna);
category_choosen = itemView.findViewById(R.id.category_qna);
question_asked = itemView.findViewById(R.id.question_qna);
upvote = itemView.findViewById(R.id.upvote_waala_qna);
downvote = itemView.findViewById(R.id.downvote_waala_qna);
upvote_liner = itemView.findViewById(R.id.upvote_linear);
// upimage = itemView.findViewById(R.id.upvote_button);
}
public qna_data getItem(int position) {
// Get the question object at this position
DataSnapshot questionSnapshot = dataSnapshotList.get(position);
qna_data question = questionSnapshot.getValue(qna_data.class);
question.setPushkey(questionSnapshot.getKey());
return question;
}
}
}
我在调试后更新了代码,我知道values变量的值更改为1,但情况1:如果使用setValue()方法,则不运行,但如果不使用setValue()方法,则运行良好
1条答案
按热度按时间7kqas0il1#
因为在case 0代码块中values的值永远不会更新,所以它将在后续单击时始终执行case 0代码块,从而导致每次递增likeCount的值。
要解决这个问题,你需要在case 0块执行后更新values的值,你可以在case 0块的
answersRef.child("upvote").setValue(likeCount);
后面加上values = 1;
,如下所示:这将确保在case 0之后的下一次单击时执行case 1,反之亦然,允许您根据需要增加或减少upvote值。