Android的 - 存储/检索与共享preferences串 [英] Android - Storing/retrieving strings with shared preferences
问题描述
正如标题所说,我要保存和检索某些字符串。但我的code将不通过第一线通无论是在检索或存储。 我想请点击此链接:<一href="http://developer.android.com/guide/topics/data/data-storage.html">http://developer.android.com/guide/topics/data/data-storage.html
私人无效savepath(字符串pathtilsave,int i)以{
字符串TAL = NULL;
//不走过去的线下
共享preferences.Editor编辑= GET preferences(MODE_PRIVATE).edit();
TAL =将String.valueOf(我);
editor.putString(TAL,pathtilsave);
editor.commit();
}
和我的检索方式:
公共无效getpaths(){
字符串TAL = NULL;
//不走过去的线下
共享preferences设置= getShared preferences(preFS_NAME,0);
的for(int i = 1; I&LT; = lydliste.length - 1;我++){
TAL =将String.valueOf(我);
字符串restoredText = settings.getString(TAL,NULL);
如果(restoredText!= NULL){
lydliste [我] = restoredText;
}
}
}
lydliste是一个静态的字符串数组。 preFS_NAME
是
公共静态最后弦乐preFS_NAME =我的prefsFile;
我解决了! 这时候我打电话从类中的方法没有工作!我不得不从另一个类由于某种原因调用它,写的classname.this作为Context参数。 这里是最后的工作:
共享preferences设置= ctx.getShared preferences(preFS_NAME,0);
设置= ctx.getShared preferences(preFS_NAME,0);
共享preferences.Editor编辑器= settings.edit();
editor.putString(TAL,pathtilsave);
editor.commit();
As the title says, I want to save and retrieve certain strings. But my code won't pass through the first line neither in retrieve or store. I tried to follow this link: 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();
}
and my retrieve method:
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 is a static string array. PREFS_NAME
is
public static final String PREFS_NAME = "MyPrefsFile";
I solved it! It didn't work when I called the methods from within the class! I had to call it from another class for some reason, and write "classname.this" as Context parameter. Here's the final working:
SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, 0);
settings = ctx.getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(tal, pathtilsave);
editor.commit();
这篇关于Android的 - 存储/检索与共享preferences串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!