2

Just my question, how to save Arraylists and tables in SharedPreferences?

I specify that the backup is necessary to the operation of the application and will be offline.

int REPONSE[]= new int[5];

And

ArrayList CATEGORIE1 = new ArrayList();

My data to save this int arraylist.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Leic
  • 57
  • 8
  • As others mentioned you can use Gson or any other serialization tools. Also you can make your classes Serializable or Parcelable and the put them directly. Also if you are interested I've already created a simple library which makes this process super easy you can take a look at it: [https://github.com/amin-amini/EasyPrefs](https://github.com/amin-amini/EasyPrefs) – Amin Jan 19 '19 at 23:37

2 Answers2

4

save data object in SharedPreferences

    import android.content.Context;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.text.TextUtils;

    import org.json.JSONArray;
    import org.json.JSONException;

    import java.util.ArrayList;
    import java.util.List;


    public class ListToSharedPreferences extends AppCompatActivity {
        private SharedPreferences prefs;

        private List<Integer> selectedIntegers = new ArrayList<>();

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            prefs = getSharedPreferences("myprefs", Context.MODE_PRIVATE);
        }


        @Override
        protected void onResume() {
            super.onResume();
            loadStateFromPrefs();
        }

        @Override
        protected void onPause() {
            super.onPause();
            storeStateToPrefs();
        }

        private void loadStateFromPrefs() {
            if (prefs == null) {
                return;
            }


            // read in json array from prefs to fill list
            selectedIntegers = new ArrayList<>();
            String jsonArrayString = prefs.getString("selectedIntegers", "");
            if (!TextUtils.isEmpty(jsonArrayString)) {
                try {
                    JSONArray jsonArray = new JSONArray(jsonArrayString);
                    if (jsonArray.length() > 0) {
                        for (int i = 0; i < jsonArray.length(); i++) {
                            Integer integer = jsonArray.getInt(i);
                            selectedIntegers.add(integer);
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        }

        protected void storeStateToPrefs() {
            if (prefs == null) {
                return;
            }
            SharedPreferences.Editor editor = prefs.edit();

            // store list as jsonarray
            JSONArray jsonArray = new JSONArray();
            for (Integer z : selectedIntegers) {
                jsonArray.put((int) z);
            }
            editor.putString("selectedIntegers", jsonArray.toString());
            editor.commit();
        }
    }
Shomu
  • 2,734
  • 24
  • 32
1

You can use gson library for this.

// How to store JSON string
Gson gson = new Gson();
// This can be any object. Does not have to be an arraylist.
String json = gson.toJson(myAppsArr);

// How to retrieve your Java object back from the string
Gson gson = new Gson();
DataObject obj = gson.fromJson(arrayString, ArrayList.class);
vaibhav kumar
  • 414
  • 2
  • 11