存储 json 数据 sharedpreference 且不覆盖
Store json data sharedpreference and not overwrite
我创建了应用程序,当用户添加新数据时,会有新标签
我已经尝试过并且有效,但我想知道如何制作存储在 SharedPreferences 中的 json
不要过度书写
所以我可以将 2 个或更多用户 json 添加到适配器
这是我的字符串文件 json 变量包含添加的用户
SharedPreferences sharedPreferences = getSharedPreferences("newUser", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("listNewUser",json)
editor.apply();
这是我如何从共享偏好
中获取json
try{
String listNewUserAdd = sh.getString("listNewUser","");
JSONObject object = new JSONObject(listNewUserAdd);
for (int i=0; i<object.length(); i++) {
CustomerNew customer = new CustomerNew();
customer.setCustomerName(object.getString("receiverName"));
customer.setAccountId(object.getString("customerReference"));
customer.setId(object.getLong("customerId"));
System.out.println("### GET CUSTOMER NAME "+customer.getCustomerName());
listSortNew.add(customer);
if (listSortNew == null) {
// if the array list is empty
// creating a new array list.
listSortNew = new ArrayList<>();
}
}
我觉得有两种实现方式。一种方法是当你输入数据时,你必须检查它是否存在。
SharedPreferences sharedPreferences = getSharedPreferences("newUser", MODE_PRIVATE);
String listNewUser = sharedPreferences.getString("listNewUser","");
if(!TextUtils.isEmpty(listNewUser)){
//covert it the list object
//then add the all new item to it
//finally convert it to json string
}
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("listNewUser",json)
editor.apply();
另一种方法是改变 SharedPreferences
MODE_APPEND
but you must know, it doesn't mean that you add multiple values for each key. It means that if the file already exists it is appended to and not erased. We usually used MODE_PRIVATE
最后我建议你先从里面获取数据,然后检查是否需要更改,你可以更改数据,然后重新保存。
我创建了应用程序,当用户添加新数据时,会有新标签 我已经尝试过并且有效,但我想知道如何制作存储在 SharedPreferences 中的 json 不要过度书写 所以我可以将 2 个或更多用户 json 添加到适配器
这是我的字符串文件 json 变量包含添加的用户
SharedPreferences sharedPreferences = getSharedPreferences("newUser", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("listNewUser",json)
editor.apply();
这是我如何从共享偏好
中获取json try{
String listNewUserAdd = sh.getString("listNewUser","");
JSONObject object = new JSONObject(listNewUserAdd);
for (int i=0; i<object.length(); i++) {
CustomerNew customer = new CustomerNew();
customer.setCustomerName(object.getString("receiverName"));
customer.setAccountId(object.getString("customerReference"));
customer.setId(object.getLong("customerId"));
System.out.println("### GET CUSTOMER NAME "+customer.getCustomerName());
listSortNew.add(customer);
if (listSortNew == null) {
// if the array list is empty
// creating a new array list.
listSortNew = new ArrayList<>();
}
}
我觉得有两种实现方式。一种方法是当你输入数据时,你必须检查它是否存在。
SharedPreferences sharedPreferences = getSharedPreferences("newUser", MODE_PRIVATE);
String listNewUser = sharedPreferences.getString("listNewUser","");
if(!TextUtils.isEmpty(listNewUser)){
//covert it the list object
//then add the all new item to it
//finally convert it to json string
}
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("listNewUser",json)
editor.apply();
另一种方法是改变 SharedPreferences
MODE_APPEND
but you must know, it doesn't mean that you add multiple values for each key. It means that if the file already exists it is appended to and not erased. We usually used MODE_PRIVATE
最后我建议你先从里面获取数据,然后检查是否需要更改,你可以更改数据,然后重新保存。