I want to save this array list data in sharedpreferences and access that data in another fragment .Please Give me suggestion
Array list content given bellow
public String productname;
public String productunit;
public String productquantity;
public String productprice;
public String productdiscount;
答案 0 :(得分:0)
Here is solution, Step 1: Create a class like
SharedPreference
public static final String FAVORITES = "PRODUCTS";
private SharedPreferences settings;
private Editor editor;
public SharedPreference() {
super();
}
Now code to save ArrayList
public void saveArrayList(Context context, List<String> unread_ids) { settings = context.getSharedPreferences(AppConfig.KEY_PREFS_NAME, Context.MODE_PRIVATE); editor = settings.edit(); Gson gson = new Gson(); String jsonFavorites = gson.toJson(unread_ids); editor.putString(FAVORITES, jsonFavorites); editor.apply(); }
Now code to get saved Arraylist
public ArrayList<String> getSavedList(Context context) {
// SharedPreferences settings;
List<String> unReadId;
settings = context.getSharedPreferences(AppConfig.KEY_PREFS_NAME,
Context.MODE_PRIVATE);
if (settings.contains(FAVORITES)) {
String jsonFavorites = settings.getString(FAVORITES, "");
Gson gson = new Gson();
String[] favoriteItems = gson.fromJson(jsonFavorites,
String[].class);
unReadId = Arrays.asList(favoriteItems);
unReadId = new ArrayList<>(unReadId);
} else {
return new ArrayList<String>();
}
return (ArrayList<String>) unReadId;
}
Code to save list:
sharedPreferences.saveArrayList(context, <YOUR LIST NAME>);
Now code to get your Arraylist in other Fragment
<LIST NAME> = sharedPreference.getSavedList(getActivity());
Before get and save array list you have to declare "sharedPreference" and create its object.
Hope this will help you.
答案 1 :(得分:0)
我之前做了类似的事情,请检查此代码
private List<String> list; /// adding checked items to list
在您的OnCreate()
内添加此行
list = new ArrayList<>(); /// checked items list
要从模型中获取数组并将其存储在本地,请使用下一步。
int searchListLength = items.size();
for (int i = 0; i < searchListLength; i++) {
if (items.get(i).isChecked()) {
items.get(i).getId();
Log.d("listitem", String.valueOf("id=" + items.get(i).getId()));
list.add("id=" + items.get(i).getId());
}
}
StringBuilder stringBuilder = new StringBuilder();
for (String s : list) {
stringBuilder.append(s);
stringBuilder.append("&");
}
SharedPreferences notif_array = getSharedPreferences("items_array", Context.MODE_PRIVATE);
SharedPreferences.Editor editor_notaif = notif_array.edit();
editor_notaif.putString("id", stringBuilder.toString());
editor_notaif.apply();
Intent intent = new Intent(MainActivity.this, Filtered_Activity.class);
startActivity(intent);
在另一个活动/片段中
私有字符串new_result;
SharedPreferences notif_array = getSharedPreferences("items_array", Context.MODE_PRIVATE);
new_result = notif_array.getString("id", null);
答案 2 :(得分:0)
您可以通过3个简单的步骤使用PowerPreference
库来做到这一点!
https://github.com/AliEsaAssadi/Android-Power-Preference
ArrayList<String> list = new ArrayList<>();
list.add("example");
list.add("anotherText");
PowerPreference.defult().put("list",list);
ArrayList<String> list = PowerPreference.defult()
.getObject("list", ArrayList.class);