简体   繁体   中英

How can I save a ArrayList of custom objects after the app closes?

I already looked for an answer here but none applied to my particular case. I have an arrayList

ArrayList<GridItem> gridItems = new ArrayList<>();

To which the user can add entries through interacting with the app. I understand that SharedPreferences doesn't work with objects and I can't get gson to work.

I would like to save the arraylist in onPause and look for a preexisting saved list in onCreate. Is this the correct approach?

EDIT: I should clarify that each entry is made of two string. This is the obj constructor:

public GridItem(String Name, String Path){
        mName = Name;
        mPath = Path;
    }

so each entry is basically like this:

gridItems.add("a name", "/sdcard/emulated etc etc")

You can either try storing it in a SQL database. Or somehow concatenate string in SharedSettings. For complex data i would do the SQLite database.

Yes it is correct approach. For SharedPreferences you can look save-arraylist-to-sharedpreferences

For Sqlite you can read this questions : saving-arraylists-in-sqlite-databases and how-to-save-my-arraylist-into-sqlite-database

And for Realm you can look this question listobject-or-realmlistrealmobject-on-realm-android and go for Realm Android documentation : Realm Android

If you ask me which is easiest way i will tell you to use SharedPreferences but if you have huge amount of data you can store and manage them easily with a database.

If you want to make choice between Sqlite and Realm i cant tell you anything because it depends on you.If you good at and know about Sql you can go for Sqlite because in Sqlite we are using Sql commands,queries.But i am using Realm in my project. You can read this page : 5-reasons-why-you-should-choose-realm-over-coredata . In Realm you must learn Pojo and RealmObject .

Summary : Easiest way is using SharedPreferences but its good for small amount of datas.

If you have large amount of data you must use database to manage them easily. If you know about Sql go for Sqlite , if you know about Pojo classes and like them go for Realm

So I managed to get it working, It was a mixture of a lot of code. First of all in the onCreate i initialize the ArrayList, and if there is some data to restore it does the work, otherwise it creates an empy ArrayList.

In onCreate
    // Create an ArrayList of GridItem objects
            gridItems = new ArrayList<>(); // Now gridItems = []


            SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
            Gson gson = new Gson();
            String json = sharedPrefs.getString(TAG2, null); //Retrieve previously saved data
            if (json != null) { 
                Type type = new TypeToken<ArrayList<GridItem>>() {}.getType();
                gridItems = gson.fromJson(json, type); //Restore previous data
            }
    //Initialize the view
    //NOTE if you pass a null ArrayList<GridItem> the app will crash 
            gridAdapter = new GridItemAdapter(this, gridItems); 
//etc etc

In on pause i take the ArrayList actually present on screen and place it in json form. Then I save that json value and use it in OnCreate

In onPause

//Set the values
        SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
        SharedPreferences.Editor editor = sharedPrefs.edit();
        Gson gson = new Gson();

        String json = gson.toJson(gridItems); //Convert the array to json

        editor.putString(TAG2, json); //Put the variable in memory
        editor.commit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM