2016-08-16 61 views
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); 
        } 

       } 
      } 
     }); 
    } 
    } 
}); 

回答

0

我是你正在使用的圖書館的創建者:SimpleRatingBar

您需要在onRatingChanged方法中使用boolean fromUser的值。這具有相同的含義,你會從普通的Android的RatingBar期待:

  • 真要是評級改變,因爲用戶操作(通過觸摸評價吧)
  • 假,如果評級編程改變(通過調用setRating或使用動畫)

所以,在你的代碼:

@Override 
public void onRatingChanged(SimpleRatingBar simpleRatingBar, float rating, boolean fromUser) { 
    if (fromUser) { 
     // user changed the rating by touching the rating bar 

     double avgSum = ratingSimplePost.getAverageRating() * ratingSimplePost.getParticipantCount(); 
     double myRating = simpleRatingBar.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); 
        } 

       } 
      } 
     }); 
     } 
    } 
    } 

另外,請記住,有一個bug其原因d庫總是在fromUser的值中返回false。 這將在版本0.1.1中更正,我將在幾分鐘後發佈。

您可以從jcenterMaven Central下載此版本。只需添加到您的build.gradle文件中:

dependencies { 
    compile 'com.iarcuschin:simpleratingbar:0.1.+' 
} 
+0

謝謝!你太棒了! –