简体   繁体   中英

Android refresh rating bar upon triggeration

I am having some problem when trying to refresh the rating bar after user submitted their rating. So basically I am passing the existing rating amount when certain button on my other Activity was triggered:

viewDtlEventBtn.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            Object[] obj = new Object[2];
            obj[0] = String.valueOf(eventIDTV.getText());
            obj[1] = eventReviewModel;
            new GetEventDetailAsyncTask(new GetEventDetailAsyncTask.OnRoutineFinished() {
                public void onFinish() {
                    // Passing whole object with value into another activity
                    Intent eventDtlIntent = new Intent(context, EventDetailMain.class);
                    // Pass in a list of rating star together with amount
                    eventDtlIntent.putExtra("eventPopulateStarObj", populateRatingStar);
                    context.startActivity(eventDtlIntent);
                }
            }).execute(obj);
        }
    });

And I am populating the rating bar when onCreate():

ratingStarList = (ArrayList<EventReview>) i
            .getSerializableExtra("eventPopulateStarObj");

public void populateRatingProgressBar() {
    int totalStar = 0;
    // Get the total amount of rate records
    for (int j = 0; j < ratingStarList.size(); j++) {
        if (ratingStarList.get(j).getStarAmt() != null) {
            totalStar += Integer.parseInt(ratingStarList.get(j)
                    .getStarAmt());
        }
    }

    txtTotalRate.setText(totalStar + " Ratings for this event");        
    // Set progress bar based on the each rates
    for (int i = 0; i < ratingStarList.size(); i++) {
        if (ratingStarList.get(i).getStarAmt() != null) {
            if (ratingStarList.get(i).getEventReviewRate().equals("5")) {
                pb5Star.setProgress(Integer.parseInt(ratingStarList.get(i)
                        .getStarAmt()));
            } else if (ratingStarList.get(i).getEventReviewRate()
                    .equals("4")) {
                pb4Star.setProgress(Integer.parseInt(ratingStarList.get(i)
                        .getStarAmt()));
            } else if (ratingStarList.get(i).getEventReviewRate()
                    .equals("3")) {
                pb3Star.setProgress(Integer.parseInt(ratingStarList.get(i)
                        .getStarAmt()));
            } else if (ratingStarList.get(i).getEventReviewRate()
                    .equals("2")) {
                pb2Star.setProgress(Integer.parseInt(ratingStarList.get(i)
                        .getStarAmt()));
            } else if (ratingStarList.get(i).getEventReviewRate()
                    .equals("1")) {
                pb1Star.setProgress(Integer.parseInt(ratingStarList.get(i)
                        .getStarAmt()));
            }
        }
    }
}

It did populated correctly. However, I not sure how to refresh the rating bar after user submitted their rating. Here is the code when user submit their rating:

public void promptSubmitStar() {
    AlertDialog.Builder Dialog = new AlertDialog.Builder(getActivity());
    Dialog.setTitle("Confirm Rating");
    LayoutInflater li = (LayoutInflater) getActivity().getSystemService(
            Context.LAYOUT_INFLATER_SERVICE);
    View dialogView = li.inflate(R.layout.option_submit_star, null);
    txtPromptStarRate = (TextView) dialogView
            .findViewById(R.id.txtPromptStarRate);
    txtPromptStarRate.setText("Confirm to submit " + starRate
            + " stars for this event?");
    Dialog.setView(dialogView);
    Dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            EventReview eventReviewModel = new EventReview();
            eventReviewModel.setEventID(eventID);
            eventReviewModel.setEventReviewBy(userID);
            eventReviewModel.setEventReviewRate(String.valueOf(starRate));
            new CreateEventReviewAsyncTask(context)
                    .execute(eventReviewModel);
            dialog.dismiss();
            // Disable the rating bar by setting a touch listener which
            // always return true
            ratingBar.setOnTouchListener(new OnTouchListener() {
                public boolean onTouch(View view, MotionEvent event) {
                    return true;
                }
            });
        }
    });

    Dialog.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    dialog.dismiss();
                }
            });
    Dialog d = Dialog.show();
    EventDialogueBox.customizeDialogueBox(context, d);
}

Any ideas? Thanks in advance.

Use setRating (starRate); to programmatically set the rating on the RatingBar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM