0
我正試圖找到另一種替代解決方案來檢查用戶是否對帖子進行了評分。評分欄Android替代方法OnRatingBarChangeListener?
問題是,如果我使用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);
}
}
}
});
}
}
});
謝謝!你太棒了! –