我正在开发一个Android应用程序。我想在其中显示一个屏幕,以允许用户保存未知数量的字符串
稍后将出现在菜单中。我通过使用SharedPreferences
创建和更新字符串集来做到这一点。
问题:
直到我转到另一个活动,由于某种原因,存储在共享preferences
中的字符串集 presets 现在仅包含存储的第一个字符串,它似乎工作正常。当我参加创建/更新的活动时
共享首选项字符串集预设包含所有这些首选项。
以下是我的代码:
记录消息,显示原始和第二个活动中字符串集的内容
public void bSave(View view) {
Set<String> presets;
Integer HOR_RES = Integer.parseInt(editTextHor.getText().toString());
Integer VERT_RES = Integer.parseInt(editTextVert.getText().toString());
String str = (editTextTitle.getText().toString()+HOR_RES + "x" + VERT_RES);
SharedPreferences settings = getSharedPreferences("Share", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = settings.edit();
presets = settings.getStringSet("presets", new HashSet<String>());
presets.add(str);
Log.e("f u", presets.toString());
edit.putStringSet("presets",presets);
edit.apply();
Log.e("f u", settings.getStringSet("presets", new HashSet<String>()).toString());
}
AND
SharedPreferences settings = getSharedPreferences("Share", Context.MODE_PRIVATE);
Set<String> strings = settings.getStringSet("presets", new HashSet<String>());
Log.e("f u", settings.getStringSet("presets", new HashSet<String>()).toString());
AND
2018-11-29 13:37:53.107 12030-12030/screenresizerpremiumv2.andrewdaw.com.screenresizerpremium E/MemoryLeakMonitorManager: MemoryLeakMonitor.jar is not exist!
2018-11-29 13:37:53.110 12030-12030/screenresizerpremiumv2.andrewdaw.com.screenresizerpremium E/Minikin: Could not get cmap table size!
2018-11-29 13:37:53.755 12030-12053/screenresizerpremiumv2.andrewdaw.com.screenresizerpremium E/vndksupport: Could not load vendor/lib64/egl/libGLES_mali.so from sphal namespace: dlopen failed: library "vendor/lib64/egl/libGLES_mali.so" not found.
2018-11-29 13:37:53.782 12030-12030/screenresizerpremiumv2.andrewdaw.com.screenresizerpremium E/f u: []
2018-11-29 13:38:11.938 12030-12030/screenresizerpremiumv2.andrewdaw.com.screenresizerpremium E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
2018-11-29 13:38:11.938 12030-12030/screenresizerpremiumv2.andrewdaw.com.screenresizerpremium E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
2018-11-29 13:38:11.974 12030-12030/screenresizerpremiumv2.andrewdaw.com.screenresizerpremium E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
2018-11-29 13:38:18.235 12030-12030/screenresizerpremiumv2.andrewdaw.com.screenresizerpremium E/f u: [11x1]
2018-11-29 13:38:21.548 12030-12030/screenresizerpremiumv2.andrewdaw.com.screenresizerpremium E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
2018-11-29 13:38:21.581 12030-12030/screenresizerpremiumv2.andrewdaw.com.screenresizerpremium E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
2018-11-29 13:38:21.581 12030-12030/screenresizerpremiumv2.andrewdaw.com.screenresizerpremium E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
2018-11-29 13:38:21.638 12030-12030/screenresizerpremiumv2.andrewdaw.com.screenresizerpremium E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
2018-11-29 13:38:21.638 12030-12030/screenresizerpremiumv2.andrewdaw.com.screenresizerpremium E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
2018-11-29 13:38:26.323 12030-12030/screenresizerpremiumv2.andrewdaw.com.screenresizerpremium E/f u: [22x2, 11x1]
2018-11-29 13:38:26.324 12030-12030/screenresizerpremiumv2.andrewdaw.com.screenresizerpremium E/f u: [22x2, 11x1]
AND
2018-11-29 13:38:45.544 12289-12289/? E/MemoryLeakMonitorManager: MemoryLeakMonitor.jar is not exist!
2018-11-29 13:38:45.546 12289-12289/? E/Minikin: Could not get cmap table size!
2018-11-29 13:38:46.214 12289-12329/? E/vndksupport: Could not load vendor/lib64/egl/libGLES_mali.so from sphal namespace: dlopen failed: library "vendor/lib64/egl/libGLES_mali.so" not found.
2018-11-29 13:38:46.255 12289-12289/screenresizerpremiumv2.andrewdaw.com.screenresizerpremium E/f u: [11x1]
答案 0 :(得分:0)
在遇到完善的库之前,我实际上遇到了同样的问题,并且不需要太多代码,而且代码看上去也很疯狂,FastSave:https://github.com/yehiahd/FastSave-Android 尽管具有像普通变量一样的存储能力,但它也可以将对象保存为仅一行的便捷操作。FastSave.getInstance()。saveObject(key,customObject); //用于保存自定义对象 签出...
答案 1 :(得分:0)
无法弄清楚,所以改为这样做
public void bSave(View view) {
Set<String> presets;
Integer HOR_RES = Integer.parseInt(editTextHor.getText().toString());
Integer VERT_RES = Integer.parseInt(editTextVert.getText().toString());
String str = (editTextTitle.getText().toString()+HOR_RES + "x" + VERT_RES);
SharedPreferences settings = getSharedPreferences("Share", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = settings.edit();
int i = 0;
while(!settings.getString("preset"+i,"").equals("")){
i++;
}
edit.putString("preset"+i,str);
edit.apply();
}
检索
int i = 0;
SharedPreferences settings = getSharedPreferences("Share", Context.MODE_PRIVATE);
while(!settings.getString("preset"+i,"").equals("")){
menu.add(settings.getString("preset"+i,""));
i++;
}