当我不知道密钥时,如何从共享偏好中获取值?
继承我的代码以保存到共享偏好:
String materia = data.getExtras().getString("materia");
String profesor = data.getExtras().getString("profesor");
String color = data.getExtras().getString("color");
SharedPreferences saveMateria = getSharedPreferences(
tabChangedto, MODE_PRIVATE);
SharedPreferences.Editor editor = saveMateria.edit();
editor.putString(materia, materia);
editor.putString(profesor, profesor);
editor.putString("color", color);
editor.commit();
我试过了:
ArrayList<MateriaComplex> array = new ArrayList<MateriaComplex>();
pref = getSharedPreferences(tabChangedto, MODE_PRIVATE);
Map<String, ?> keys = pref.getAll();
for (Map.Entry<String, ?> entry : keys.entrySet()) {
/*
*
* Here i want to extract the color value,materia value, profesor value.
* But i don't know how, because the key has the name of the value, to avoid replacing it.
*
*/
}
答案 0 :(得分:2)
如果您以不知道密钥的方式使用SharedPreferences
,那么您使用的是SharedPreferences
错误。
您应该考虑使用SQL数据库来存储您想要的内容