[英]android java retrieve sharedpreferences in arraylist
I have wrote a little code to save new just added items to sharedpreferences
. 我写了一些代码,将刚添加的新项目保存到
sharedpreferences
。 But if I retrieve the sharedpreferences
in an arraylist it displays {"todo":"new item" }
but I only want to display one item and only the text if you understand me. 但是,如果我在arraylist中检索
sharedpreferences
,它将显示{"todo":"new item" }
但是如果您理解我的话,我只想显示一个项目,只显示文本。 Here is my save Arraylist
code: 这是我保存的
Arraylist
代码:
public void saveArrayList(Context context ,ArrayList<DayItems> mlist){
SharedPreferences shared;
SharedPreferences.Editor editor;
shared = context.getSharedPreferences("MONDAY", Context.MODE_PRIVATE);
editor = shared.edit();
Gson gson = new Gson();
String json = gson.toJson(mlist);
editor.putString("mondAy", json);
String getSaved = shared.getString("mondAy", null);
Toast.makeText(PlannerActivity.this, getSaved, Toast.LENGTH_LONG).show();
editor.commit();
}
Here is my add item to arraylist: 这是我添加到arraylist的项目:
@Override
public void onClick(View v) {
String afspraak = addTEXT.getText().toString().trim();
if(afspraak.length()>0){
addTEXT.setText("");
mon.add(new DayItems(afspraak));
dayAdapter.notifyDataSetChanged();
//save arraylist with new item
saveArrayList(context, mon);
}
}
});
Actually what I want is a snippet to retrieve SharedPreferences
. 实际上,我想要的是一个片段以检索
SharedPreferences
。 Thanks in Advance! 提前致谢!
You can try this to retrieve from SharedPreferences 您可以尝试从SharedPreferences中检索
List<DayItems> listDayItems = gson.fromJson(shared.getString("mondAy", null), new TypeToken<List<DayItems>>() {
}.getType());
It's definitely works with your condition: 这绝对符合您的条件:
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
Gson gson = new Gson();
String json = sharedPrefs.getString(TAG, null);
Type type = new TypeToken<ArrayList<ArrayObject>>() {}.getType();
ArrayList<ArrayObject> arrayList = gson.fromJson(json, type);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.