How Do You Save User Rating In Rating Bar?
Hello all :) I have a rating bar and it works but the rating doesn't save when the user leaves the page. How do you save the user rating? Here's the code RatingBar ratingBar; Text
Solution 1:
You can use SharedPreferences for that..
Save the ratings in SharedPreferences
before leaving the app and retrieve the ratings from SharedPreferences
and set it in the ratings bar when you come back to the application..
SharedPreferences wmbPreference1,wmbPreference2;
SharedPreferences.Editor editor;
//wmbPreference for Shared Prefs that lasts forever
wmbPreference1 = PreferenceManager.getDefaultSharedPreferences(this);
//installsp for Shared Prefs that lasts only just once each time program is running
wmbPreference2 =getApplicationContext().getSharedPreferences("MYKEY",Activity.MODE_PRIVATE);
To save values
SharedPreferences.Editoreditor= wmbPreference1.edit();
editor.putString("MYKEY", "12345");
editor.commit();
You can retrieve the values like
StringPhonenumber= wmbPreference1.getString("MYKEY", "");
where MYKEY is the keyname by which you can identify the value..
EDIT
Change your code like this
SharedPreferences wmbPreference1;
SharedPreferences.Editor editor;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item_activity_1);
ratingText = (TextView) findViewById(R.id.rating);
((RatingBar) findViewById(R.id.ratingBar1))
.setOnRatingBarChangeListener(this);
wmbPreference1 = PreferenceManager.getDefaultSharedPreferences(this);
}
@OverridepublicvoidonRatingChanged(RatingBar ratingBar, float rating,
boolean fromTouch) {
finalintnumStars= ratingBar.getNumStars();
editor = wmbPreference1.edit();
editor.putInt("numStars", numStars);
editor.commit();
And when you come back do this,ie when you want to retrieve the ratings
int ratings = wmbPreference1.getInt("numStars", 0);
ratingText.setText(rating + "/" + String.valueOf(ratings));
Here ratings
will hold the ratings which was set earlier..
Post a Comment for "How Do You Save User Rating In Rating Bar?"