如何使用SharedPreferences保存更改?

时间:2019-01-17 05:42:41

标签: java android sharedpreferences

我正在开发一个应用程序,我必须保存用户所做的更改...用户要单击图像,它将更改颜色。我想保存更改。

我不确定该怎么做。这是我要进行更改的地方。

我所知道的是我需要使用 SharedPreferences

private ImageView bookmark;
bookmark = (ImageView) findViewById(R.id.imageView_bookmark_readIt);

bookmark.setOnClickListener(new View.OnClickListener(){

     private boolean bookmarking = true;

     public void onClick(View v){

      if(bookmarking){
         bookmark.setImageResource(R.drawable.ic_bookmarked_blue);
         bookmarking=false;
      }
      else{
         bookmarking=true;
         bookmark.setImageResource(R.drawable.ic_bookmark);
         //Toast.makeText(getApplicationContext(), "Changed", Toast.LENGTH_LONG).show();
       }
});

那么有人对如何保存对以上代码所做的更改有想法吗?

注意:我没有使用数据库

4 个答案:

答案 0 :(得分:0)

 /**
 * Get a shared preferences file named Const.SHARED_PREFERENCES_FILE_NAME, keys added to it must be unique
 *
 * @param ctx
 * @return the shared preferences
 */
public static SharedPreferences getSharedPreferences(Context ctx) {
    return ctx.getSharedPreferences(Const.SHARED_PREFERENCES_FILE_NAME, Context.MODE_PRIVATE);
}

public static void cacheBoolean(Context ctx, String k, Boolean v) {
    SharedPreferences prefs = getSharedPreferences(ctx);
    prefs.edit().putBoolean(k, v).apply();
}

public static Boolean getCachedBoolean(Context ctx, String k, Boolean defaultValue) {
    SharedPreferences prefs = getSharedPreferences(ctx);
    return prefs.getBoolean(k, defaultValue);
}

public static void cacheString(Context ctx, String k, String v) {
    SharedPreferences prefs = getSharedPreferences(ctx);
    prefs.edit().putString(k, v).apply();
}

public static String getCachedString(Context ctx, String k, String defaultValue) {
    SharedPreferences prefs = getSharedPreferences(ctx);
    return prefs.getString(k, defaultValue);
}

public static void cacheInt(Context ctx, String k, int v) {
    SharedPreferences prefs = getSharedPreferences(ctx);
    prefs.edit().putInt(k, v).apply();
}

public static int getCachedInt(Context ctx, String k, int defaultValue) {
    SharedPreferences prefs = getSharedPreferences(ctx);
    return prefs.getInt(k, defaultValue);
}

public static void clearCachedKey(Context context, String key) {
    getSharedPreferences(context).edit().remove(key).apply();
}

答案 1 :(得分:0)

使用SharedPreferences非常容易。您需要定义一个密钥,以后将用于检索数据。您可以存储字符串,整数,浮点数,布尔值……您需要提供上下文。

Context context = getApplicationContext();

要写入数据,请使用此代码。

SharedPreferences mPrefs = context.getSharedPreferences("YourApp", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("color", "blue");
editor.apply();

要检索数据,请使用此

SharedPreferences mPrefs = context.getSharedPreferences("YourApp", Context.MODE_PRIVATE);
String color = mPrefs.getString("color", "defaultValue");

您可以轻松地将类型从String更改为最适合您需要的其他类型。

希望有帮助。

答案 2 :(得分:0)

希望它能为您提供帮助

 SharedPreferences sharedPrefs = getSharedPreferences("SharedPreferences_Name", Context.MODE_PRIVATE);
    private ImageView bookmark;
    bookmark = (ImageView) findViewById(R.id.imageView_bookmark_readIt);
    private boolean bookmarking = sharedPrefs.getBoolean("bookmarking",true);//To get value that saved in SharedPreferences 
    if(bookmarking){
        bookmark.setImageResource(R.drawable.ic_bookmarked_blue);
    }
    else{
        bookmark.setImageResource(R.drawable.ic_bookmark);
        //Toast.makeText(getApplicationContext(), "Changed", Toast.LENGTH_LONG).show();
    }
    bookmark.setOnClickListener(new View.OnClickListener(){

       // private boolean bookmarking = true;

        public void onClick(View v){

            if(bookmarking){
                bookmark.setImageResource(R.drawable.ic_bookmarked_blue);
                bookmarking=false;
                SharedPreferences.Editor editor = getSharedPreferences("SharedPreferences_Name", 0).edit();
                editor.putBoolean("bookmarking", bookmarking);
                editor.apply();
            }
            else{
                bookmarking=true;
                bookmark.setImageResource(R.drawable.ic_bookmark);
                //Toast.makeText(getApplicationContext(), "Changed", Toast.LENGTH_LONG).show();
                SharedPreferences.Editor editor = getSharedPreferences("SharedPreferences_Name", 0).edit();
                editor.putBoolean("bookmarking", bookmarking);
                editor.apply();
            }
        });

答案 3 :(得分:0)

在共享首选项中,数据以“键值”格式存储。据我了解,您需要保存两个字段,这将是这样的:

“booking: true”
“bookmarkImageResource: 15670341274”

这是一种方便的方法:

第一步 –创建一个新的类SharedPrefs:

public class SharedPrefs {

private static final String SHARED_PREFERENCES_NAME = "user_prefs";
private static final String BOOKING_INFO = "booking";
private static final String BOOKMARK_IMAGE_RESOURCE_INFO = "bookmarkImageResource";
private final SharedPreferences prefs;

public SharedPrefs(Context context) {
    prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
}

public boolean saveBookingInfo(String bookingInfo, String bookmarkImageResource) {
    return prefs.edit()
            .putString(BOOKING_INFO, bookingInfo)
            .putString(BOOKMARK_IMAGE_RESOURCE_INFO, bookmarkImageResource)
            .commit();
}

public Pair<String, String> getBookingInfo() {
    return new Pair<String, String>(
            prefs.getString(BOOKING_INFO, ""),
            prefs.getString(BOOKMARK_IMAGE_RESOURCE_INFO, ""));
}

public void clearAll() {
    prefs.edit().clear().apply();
}

}

第二步-在需要保存,获取或清除数据的任何地方使用课堂! 如果您这样:

    SharedPrefs prefs = new SharedPrefs(this);  // or getActivity() instead of this if we are in a fragment      
    if(bookmarking){
        bookmark.setImageResource(R.drawable.ic_bookmarked_blue);
        bookmarking=false;
    }
    else{
        bookmarking=true;
        bookmark.setImageResource(R.drawable.ic_bookmark);
    }
    prefs.saveBookingInfo(String.valueOf(bookmarking), String.valueOf(bookmark));

希望这对您有帮助=)

祝您有美好的一天,并祝您编程愉快!