如何在Android中使用SharedReferences来存储、获取和编辑值?

如何在Android中使用SharedReferences来存储、获取和编辑值?,android,sharedpreferences,Android,Sharedpreferences,我想存储一个分数和名称值,并需要在用户获得更高分数时检索和编辑它。我该怎么做?有人能帮我用一下密码吗 MainActivity.java package com.thesis.boggleit; 导入java.util.ArrayList; 导入java.util.concurrent.TimeUnit; 导入android.annotation.SuppressLint; 导入android.annotation.TargetApi; 导入android.app.Activity; 导入an

我想存储一个分数和名称值,并需要在用户获得更高分数时检索和编辑它。我该怎么做?有人能帮我用一下密码吗

MainActivity.java

package com.thesis.boggleit;
导入java.util.ArrayList;
导入java.util.concurrent.TimeUnit;
导入android.annotation.SuppressLint;
导入android.annotation.TargetApi;
导入android.app.Activity;
导入android.content.Intent;
导入android.content.SharedReferences;
导入android.os.Build;
导入android.os.Bundle;
导入android.os.CountDownTimer;
导入android.util.Log;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.view.Window;
导入android.view.WindowManager;
导入android.widget.ArrayAdapter;
导入android.widget.AutoCompleteTextView;
导入android.widget.EditText;
导入android.widget.ImageButton;
导入android.widget.ImageView;
导入android.widget.ListView;
导入android.widget.TextView;
公共类MainActivity扩展了活动{
//这里的变量
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
dbHelper=新的DBAdapter(this);
search=(EditText)findViewById(R.id.textHere);
OnClickListener mycommocClickListner=新的OnClickListener(){
@凌驾
公共void onClick(视图arg0){
Log.i(标记“arg0.getId()=”+arg0.getId());
if(arg0.getId()==R.drawable.a){
Log.i(标记“arg0.getId()=”+arg0.getId());
generatedString=generatedString+(“a”);
text.setText(generatedString);
((ImageButton)arg0.setImageResource(R.drawable.changea);
如果(!timeHasStarted){
countDownTimer.start();
timeHasStarted=true;
}
}
};
图1.setOnClickListener(MyComMoClickListener);
}
//这里是计时器
私有int optionxtview=0;
私有int addClick=0;
私有无效计算(){
x=Integer.parseInt(tv3.getText().toString().replaceAll(“\\s”,“”));
y=Integer.parseInt(tv2.getText().toString().replaceAll(“\\s”,“”));
z=x+y;
score.setText(整数.toString(z));
}
字符串s1=search.getText().toString();
字符串s2=dbHelper.getData(s1);
........
}
文件明确指出

如果要保存的键值集合相对较小,则应使用SharedReferences API。SharedReferences对象指向包含键值对的文件,并提供读取和写入键值对的简单方法。每个SharedReferences文件由框架管理,可以是私有的或共享的

这意味着您必须使用
SharedReferences
类来实现您想要的功能

例如:-

SharedReferences sharedPref=getActivity().getPreferences(Context.MODE\u PRIVATE);
//在片段中

SharedReferences sharedPref=getApplicationContext().getPreferences(Context.MODE\u PRIVATE);
//在活动中


最好的学习方法是访问上面的链接并阅读。

存储值

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("name", name);
editor.putInt("score", score);
editor.commit();
String name = prefs.getString("name", "");
int score = prefs.getInt("score", 0);
获取价值

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("name", name);
editor.putInt("score", score);
editor.commit();
String name = prefs.getString("name", "");
int score = prefs.getInt("score", 0);
使用此公共类:

public static boolean setPreference(Context context, String key, int value) {

    if (context == null) {
        throw new IllegalArgumentException("'context' must not be null.");
    }
    if (key == null) {
        throw new IllegalArgumentException("'key' must not be null.");
    }

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putInt(key, value);
    return editor.commit();
} 
public static int getPreference(Context context, String key, int defValue) {

    if (context == null) {
        throw new IllegalArgumentException("'context' must not be null.");
    }
    if (key == null) {
        throw new IllegalArgumentException("'key' must not be null.");
    }

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    return preferences.getInt(key, defValue);
}
如何使用:

int currentScore = 20; 
    PreferenceUtil.setPreference(context, "key1", currentScore);

    //get. If the key1 in not available, return default value 0

    int myScore = PreferenceUtil.getPreference(context, "key1", 0);

尝试使用此重复实验室,在保存或获取过程中是否出现任何错误???每当我放置sharedpref cde时,活动将不会加载。