我正在尝试寻找另一种替代解决方案来检查用户是否在帖子上评分。
问题是,如果我使用 OnRatingBarChangeListener 另一个用户可能会投票并更改将触发事件的评级。
我尝试过 onClickListener 和 OnTouchListener 并没有真正成功,因为它们覆盖了初始方法并且无法与评级栏正确交互......
有什么建议么?
if (ratingSimplePost.getDidRate() == true) {
ratingViewHolder.starRating.setRating((float)ratingSimplePost.getAverageRating());
ratingViewHolder.starRating.isIndicator();
}else if(ratingSimplePost.getDidRate() == false){
ratingSimplePost.setDidRate(true);
ratingViewHolder.starRating.setOnRatingBarChangeListener(new SimpleRatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(SimpleRatingBar simpleRatingBar, float rating, boolean fromUser) {
if (ratingViewHolder.starRating.getRating() == 0) {
} else {
ratingViewHolder.starRating.setRating(ratingViewHolder.starRating.getRating());
double avgSum = ratingSimplePost.getAverageRating() * ratingSimplePost.getParticipantCount();
double myRating = ratingViewHolder.starRating.getRating();
myRating = (Math.round(myRating * 100));
myRating = myRating / 100;
double newRating = (avgSum + myRating) / (ratingSimplePost.getParticipantCount() + 1);
newRating = (Math.round(newRating * 100));
newRating = newRating / 100;
ParseQuery<ParseObject> updateRating = ParseQuery.getQuery("Posts");
updateRating.whereEqualTo("objectId", ratingSimplePost.getPostId());
final double finalNewRating = newRating;
final double finalMyRating = myRating;
ratingSimplePost.setAverageRating(newRating);
updateRating.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
for (int i = 0; i < objects.size(); i++) {
ParseObject object = objects.get(i);
object.put("AvgRating", finalNewRating);
ArrayList<Double> ratingValues = (ArrayList<Double>) object.get("Ratings");
ratingValues.add((double) finalMyRating);
object.put("Ratings", ratingValues);
ratingSimplePost.setNewRating(finalNewRating);
ArrayList<String> partList = (ArrayList<String>) object.get("Participants");
partList.add(ParseUser.getCurrentUser().getUsername());
object.put("Participants", partList);
object.saveInBackground();
ratingViewHolder.starRating.setRating((float) finalNewRating);
}
}
}
});
}
}
});