Android – 存储/检索具有共享首选项的string

正如标题所说,我想保存并检索某些string。 但是我的代码不能通过检索或存储的第一行。 我试图按照这个链接: http : //developer.android.com/guide/topics/data/data-storage.html

private void savepath(String pathtilsave, int i) { String tal = null; // doesn't go past the line below SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit(); tal = String.valueOf(i); editor.putString(tal, pathtilsave); editor.commit(); } 

和我的检索方法:

 public void getpaths() { String tal = null; // doesn't go past the line below SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); for (int i = 1; i <= lydliste.length - 1; i++) { tal = String.valueOf(i); String restoredText = settings.getString(tal, null); if (restoredText != null) { lydliste[i] = restoredText; } } } 

lydliste是一个静态string数组。 PREFS_NAME

 public static final String PREFS_NAME = "MyPrefsFile"; 

保存到首选项:

 PreferenceManager.getDefaultSharedPreferences(context).edit().putString("MYLABEL", "myStringToSave").apply(); 

获取存储的首选项:

 PreferenceManager.getDefaultSharedPreferences(context).getString("MYLABEL", "defaultStringIfNothingFound"); 

context是你的上下文。


如果您获得多个值,重用相同的实例可能会更有效。

  SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); String myStrValue = prefs.getString("MYSTRLABEL", "defaultStringIfNothingFound"); Boolean myBoolValue = prefs.getBoolean("MYBOOLLABEL", false); int myIntValue = prefs.getInt("MYINTLABEL", 1); 

如果你正在保存多个值:

 Editor prefEditor = PreferenceManager.getDefaultSharedPreferences(context).edit(); prefEditor.putString("MYSTRLABEL", "myStringToSave"); prefEditor.putBoolean("MYBOOLLABEL", true); prefEditor.putInt("MYINTLABEL", 99); prefEditor.apply(); 

注意:使用apply()保存比使用commit()更好。 唯一需要commit()是如果你需要返回值,这是非常罕见的。

 private static final String PREFS_NAME = "preferenceName"; public static boolean setPreference(Context context, String key, String value) { SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); SharedPreferences.Editor editor = settings.edit(); editor.putString(key, value); return editor.commit(); } public static String getPreference(Context context, String key) { SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); return settings.getString(key, "defaultValue"); } 

我解决了它! 当我从课堂上调用这些方法的时候,这是行不通的! 我不得不从另一个类中调用它,并将“classname.this”作为Context参数。 这是最后的工作:

 SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, 0); settings = ctx.getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putString(tal, pathtilsave); editor.commit(); 

如果你不关心commit()的返回值,那么更好地使用apply(),因为它是asynchronous的commit()更快

 final SharedPreferences prefs = context.getSharedPreferences("PREFERENCE_NAME", Context.MODE_PRIVATE); SharedPreferences.Editor editor = prefs.edit(); editor.putString("key", "value"); editor.apply(); 

根据文档

与commit()不同,commit()会将其首选项同步写入持久性存储中,apply()会立即将其更改提交到内存中的SharedPreferences,但会启动对磁盘的asynchronous提交,并且不会通知任何失败。 如果此SharedPreferences上的另一个编辑器在apply()仍未完成时执行常规commit(),则commit()将会阻塞,直到完成所有asynchronous提交以及提交本身。

与上下文一起尝试:

 final SharedPreferences settings = context.getSharedPreferences( PREFS_NAME, 0); return settings.getString(key, null); 

SharedPreferences类允许您保存特定于android应用程序的首选项。

API版本11引入了方法putStringSet和getStringSet,它们允许开发人员分别存储string值列表和string值列表。

使用SharedPreferences存储string数组的示例可以这样完成:

 // Get the current list. SharedPreferences settings = this.getSharedPreferences("YourActivityPreferences", Context.MODE_PRIVATE); SharedPreferences.Editor editor = settings.edit(); Set<String> myStrings = settings.getStringSet("myStrings", new HashSet<String>()); // Add the new value. myStrings.add("Another string"); // Save the list. editor.putStringSet("myStrings", myStrings); editor.commit();