How do you SAVE user rating in rating bar?

会有一股神秘感。 提交于 2019-12-04 06:43:42

问题


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;
TextView ratingText;
SharedPreferences wmbPreference1;    
SharedPreferences.Editor editor;

@Override
protected void onCreate(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);
}

@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromTouch) {    
       final int numStars = ratingBar.getNumStars();
       editor = wmbPreference1.edit();
       editor.putInt("numStars", numStars);
       editor.commit();  
       int ratings = wmbPreference1.getInt("numStars", 0);
       ratingText.setText(rating + "/" + ratings.toString());
}

Any help would be appreciated.


回答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.Editor editor = wmbPreference1.edit();
editor.putString("MYKEY", "12345");
editor.commit();

You can retrieve the values like

String Phonenumber = 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;
@Override
protected void onCreate(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);      
}

@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromTouch) {
final int numStars = 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..



来源:https://stackoverflow.com/questions/23719641/how-do-you-save-user-rating-in-rating-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!