你如何保存评级条用户评价? [英] How do you SAVE user rating in rating bar?

查看:134
本文介绍了你如何保存评级条用户评价?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好所有:)我有一个评价吧和它的作品,但在用户离开页面时的等级不保存。你如何保存用户的评价?

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?

这里的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());
}

任何帮助将是AP preciated。

推荐答案

您可以使用共享preferences 为..

退出应用之前保存在评级共享preferences和检索共享preferences 并设置它的收视率吧,当你回来的应用程序。

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);

保存数值

SharedPreferences.Editor editor = wmbPreference1.edit();
editor.putString("MYKEY", "12345");
editor.commit();

您可以检索值

String Phonenumber = wmbPreference1.getString("MYKEY", "");

其中的myKey 键名,通过它可以识别值。

where MYKEY is the keyname by which you can identify the value..



修改

更改code这样的

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..

这篇关于你如何保存评级条用户评价?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆