How to save the RatingBar values in Android Studio?

69 Views Asked by At

I have used a RatingBar in my code, but I am having trouble saving the data. Additionally, I am encountering an issue where if I provide a rating for the first row, it is automatically taken as the rating for the seventh row.

I am using the following code:

ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
    @Override
    public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
        ratevalue=ratingBar.getRating();
1

There are 1 best solutions below

0
Cypher On

You can use SharedPreference to store rating bar values.

val preferences = getSharedPreferences("My_pref", MODE_PRIVATE)

@Override
    public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
        preferences.edit().putFloat("Rating", rating).apply()
    }

to retrieve the value from shared preference, you can do like this.

val rating = preferences.getFloat("RATING", 0.0f)

Note: you will lose your rating values if you uninstall the app.