如何比较保存到共享首选项中的 Int 值
How to compare Int value that is saved into shared preference
我正在尝试创建关卡评论并在关卡图像旁边显示完成后的关卡评级。我的代码已准备就绪,并在关卡完成后显示评级
我将分值保存到共享首选项中并检查值
这是代码
// adding value to cookies from First Activity
Editor editor = sharedpreferences.edit(); editor.putInt("level1_rating",AllGlobalVariables.level_score ); editor.putInt("level2_rating",AllGlobalVariables.level2_score );
editor.commit();
// calling function in Second Activity
rating(AllGlobalVariables.level1);
//------ function defination --------//
private void rating(String l) {
// TODO Auto-generated method stub
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
int left = 15;
int top = 65;
int right=0;
int bottom=0;
lp.setMargins(left, top, right, bottom);
if(l.equals("1")){
if (sharedpreferences.contains("level1_rating"))
{
if(AllGlobalVariables.level_score <= 50){
Bitmap goodbmp = BitmapFactory.decodeResource(getResources(), R.drawable.fail);
String goodimagecode = BitMapToStringgood(goodbmp);
Bitmap goods = StringToBitMapgoods(goodimagecode);
Drawable verticalImage = new BitmapDrawable(getResources(), goods);
AllGlobalVariables.Rimagel1 = (ImageView) findViewById(R.id.imageView6); AllGlobalVariables.Rimagel1.setImageDrawable(verticalImage);
}
}
}
}
但问题是:我能够从键中获取值,但无法比较共享偏好键中的值以显示评级。请告知如何比较键
的值
或者有什么其他想法可以做这个工作,不胜感激
感谢
SharedPreferences.contains return true
如果偏好包含偏好,否则 false
.
使用SharedPreferences.getInt获取整数值,然后与其他值进行比较:
if (sharedpreferences.contains("level1_rating"))
{
int level1_ratingValue=sharedpreferences.getInt("level1_rating",0);;
if(level1_ratingValue==70){
// do work if condition true
}else{
// do work if condition false
}
}
我正在尝试创建关卡评论并在关卡图像旁边显示完成后的关卡评级。我的代码已准备就绪,并在关卡完成后显示评级 我将分值保存到共享首选项中并检查值 这是代码
// adding value to cookies from First Activity
Editor editor = sharedpreferences.edit(); editor.putInt("level1_rating",AllGlobalVariables.level_score ); editor.putInt("level2_rating",AllGlobalVariables.level2_score );
editor.commit();
// calling function in Second Activity
rating(AllGlobalVariables.level1);
//------ function defination --------//
private void rating(String l) {
// TODO Auto-generated method stub
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
int left = 15;
int top = 65;
int right=0;
int bottom=0;
lp.setMargins(left, top, right, bottom);
if(l.equals("1")){
if (sharedpreferences.contains("level1_rating"))
{
if(AllGlobalVariables.level_score <= 50){
Bitmap goodbmp = BitmapFactory.decodeResource(getResources(), R.drawable.fail);
String goodimagecode = BitMapToStringgood(goodbmp);
Bitmap goods = StringToBitMapgoods(goodimagecode);
Drawable verticalImage = new BitmapDrawable(getResources(), goods);
AllGlobalVariables.Rimagel1 = (ImageView) findViewById(R.id.imageView6); AllGlobalVariables.Rimagel1.setImageDrawable(verticalImage);
}
}
}
}
但问题是:我能够从键中获取值,但无法比较共享偏好键中的值以显示评级。请告知如何比较键
的值或者有什么其他想法可以做这个工作,不胜感激
感谢
SharedPreferences.contains return true
如果偏好包含偏好,否则 false
.
使用SharedPreferences.getInt获取整数值,然后与其他值进行比较:
if (sharedpreferences.contains("level1_rating"))
{
int level1_ratingValue=sharedpreferences.getInt("level1_rating",0);;
if(level1_ratingValue==70){
// do work if condition true
}else{
// do work if condition false
}
}