简体   繁体   中英

How would I save the rating with a Shared Preference?

I am trying to implement a rating bar within an AlertDialog. I have the rating bar working but I can't seem to make the SharedPreference work to remember what star rating was inputted beforehand. Where/How should I put the SharedPreference to make it work?

Here is the code for the rating AlertDialog:

 private float rateValue;

  rating.setOnClickListener(clk ->{
           AlertDialog.Builder mBuild = new AlertDialog.Builder(SoccerItemDetailHostActivity.this);
           mBuild.setTitle("Please rate the app");
           View mView = getLayoutInflater().inflate(R.layout.soccerratingbar,null);

           final RatingBar ratebar = (RatingBar)mView.findViewById(R.id.ratingBar);
           ratebar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
               @Override
               public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                   rateValue = rating;
                   Toast.makeText(SoccerItemDetailHostActivity.this, ""+rating, Toast.LENGTH_SHORT).show();
               }
           });

           Button btnSubmit=(Button)mView.findViewById(R.id.btnSubRating);
           btnSubmit.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   Toast.makeText(SoccerItemDetailHostActivity.this, ""+rateValue, Toast.LENGTH_SHORT).show();
               }
           });
           mBuild.setView(mView);
           AlertDialog dialog=mBuild.create();
           dialog.show();

       });

To save the rating you need to save it in your SharedPreferences . In the code below I show you how to achieve that:

RatingBar ratingBar = findViewById(R.id.ratingbar);
        if(getRating(getApplicationContext()) != 0){
            ratingBar.setRating(getRating(getApplicationContext()));
        }
        ratingBar.setOnRatingBarChangeListener((ratingBar1, rating, fromUser) -> {
                setRatingBar(getApplicationContext(),rating);

        });

And here the mentioned SharedPreferences functions:

public static SharedPreferences prefs(Context context){
        return PreferenceManager.getDefaultSharedPreferences(context);
    }
    public static void setRatingBar(Context context, float Float) {
        prefs(context).edit().putFloat("RatingBar", Float).apply();
    }
    public static float getRating(Context context) {
        return prefs(context).getFloat("RatingBar", 0);
    }

I have the rating bar working but I can't seem to make the SharedPreference work to remember what star rating was inputted beforehand.

Well, what have you tried? Your code doesn't show any attempt at using SharedPreferences so it's impossible to say what you're doing wrong.

Where/How should I put the SharedPreference to make it work?

You could probably add an onDismissListener to the dialog that saves the current rating.

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