1

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;
  • Serialize the ArrayList using gson then save that data. Another option is to pass these data in a bundle to the fragment. – Zun Dec 03 '18 at 12:56
  • *I want to save this array list data in sharedpreferences*. it is bad idea. instance pass the `arralist` `object` using bundle. Refer Link: https://stackoverflow.com/questions/43909061/passing-object-array-list-between-fragments-in-android-development – Ali Dec 03 '18 at 12:58
  • best way send data in Argument or otherwise save data as json string in sharedpreferences, – kuber singh Dec 03 '18 at 13:03

2 Answers2

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.

Sanwal Singh
  • 1,765
  • 3
  • 17
  • 35
0

I did something similar to this before Please check this code

private List<String> list; /// adding checked items to list

inside your OnCreate() add this line

list = new ArrayList<>(); /// checked items list

and to get array from model and store it local please use next.

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);

Inside your another activity / fragment

private String new_result;

SharedPreferences notif_array = getSharedPreferences("items_array", Context.MODE_PRIVATE);
        new_result = notif_array.getString("id", null);
Hossam Hassan
  • 795
  • 2
  • 13
  • 39