質問

My problem is when i rate the same value again, it doesn't respond to the second time..That means when i rate '4' as for the first time,then i can't rate as'4' for the second time..It only responds only when i rate other value rather than '4'.

Here is what i tried( I need the action for getRatingBar in my code).

public class InteractiveRatingBarActivity extends Activity implements
       OnRatingBarChangeListener {
   RatingBar getRatingBar;
   RatingBar setRatingBar;
   TextView countText;
   int count;
   float curRate;

   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       findViewsById();

       setRatingBar.setRating(curRate);//overall rating
       getRatingBar.setOnRatingBarChangeListener(this);// ratingbar for user action. 

   }   

   private void findViewsById() {
       getRatingBar = (RatingBar) findViewById(R.id.getRating);
       setRatingBar = (RatingBar) findViewById(R.id.setRating);
       countText = (TextView) findViewById(R.id.countText);
   }


   public void onRatingChanged(RatingBar rateBar, float rating,
           boolean fromUser) {
       DecimalFormat decimalFormat = new DecimalFormat("#.#");
       curRate = Float.valueOf(decimalFormat.format((curRate * count + rating)
               / ++count));
       Toast.makeText(InteractiveRatingBarActivity.this,
               "New Rating: " + curRate, Toast.LENGTH_SHORT).show();
       setRatingBar.setRating(curRate);
       countText.setText(count + " Ratings");
   }


}

thanks in advance

役に立ちましたか?

解決 2

You can,t call the method onRatingChanged()when ever there is a change in rating bar..that is when you click the same rate for twice it may not change the rate bar, so the functions inside onRatingChanged() could not be invoked.Another possible way is call oncreate after every onRatingChanged() invoked..But it should not show the previous rate in the rating bar.

Update:
Suppose whatever you do inside onRatingChanged() is wrapped into a function, processing(). Here most probably you need is the changed rating. so we pass it as a parameter to the function. Try following way

            float newRating;
    myRatingBar.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

        newRating=myRatingBar.getRating();
        processing(newRating);   

        }
    });

他のヒント

That happens because you have OnRatingChange (if it stays the same your rating doesn't change so this method doesn't gets called) .

You can develop a OnTouchListen:

ratingBar.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    float touchPositionX = event.getX();
                    float width = ratingBar.getWidth();
                    float starsf = (touchPositionX / width) * 5.0f;
                    int stars = (int) starsf + 1;

                    DecimalFormat decimalFormat = new DecimalFormat("#.#");
                    curRate = Float.valueOf(decimalFormat.format((curRate
                            * count + starsf)
                            / ++count));
                    Toast.makeText(InteractiveRatingBarActivity.this,
                            "New Rating: " + curRate, Toast.LENGTH_SHORT)
                            .show();
                    setRatingBar.setRating(curRate);
                    countText.setText(count + " Ratings");
                    v.setPressed(false);
                }
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    v.setPressed(true);
                }

                if (event.getAction() == MotionEvent.ACTION_CANCEL) {
                    v.setPressed(false);
                }

                return true;
            }
        });
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top