0

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")
user11230
  • 539
  • 1
  • 9
  • 19
  • 2
    Yes, this is one of the approaches. You can save them to a database one by one, or make that GSON work, serialize them and save them as a string, later fetch, deserialize and voila :) – Vucko Jul 09 '16 at 09:58
  • why can't you get gson to work? can you please post your model gode? it should be pretty easy to store it as an json array string once you have proper model for gson to deserialize – bartol Jul 09 '16 at 10:15
  • Basically I can save the array but I can't restore it. Please can you point me to an example? – user11230 Jul 09 '16 at 10:42

3 Answers3

0

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.

  • It pretty simple data (see edit on my post) but the user might reach a high number of entries. I have never worked with sqlite before, so I think i will re try gson. – user11230 Jul 09 '16 at 10:41
0

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 Realmyou 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

Community
  • 1
  • 1
Yasin Kaçmaz
  • 6,573
  • 5
  • 40
  • 58
  • Thanks, as you see from my edit I have very simple data altough teh user might create a lot of entries since the nature of my app. I had already found the sharedpref link but I don't understand how to reset the array on startup, after i saved it, – user11230 Jul 09 '16 at 10:44
  • To be more specific: when he talks about addTask and TaskManagerApplicationClass what does he mean? Shouldn't I put the code into onPause? – user11230 Jul 09 '16 at 10:47
  • @user11230 if your user can be able to store huge amount of data you must go for mobile database. You are reading **How to store serialized objects to SharedPreferences** instead of arraylist. First part of answer is enough for you. – Yasin Kaçmaz Jul 09 '16 at 11:14
  • When I re open the app the array list doesn't show. In which method (onCreate or onResume) should I retrieve the saved list? – user11230 Jul 09 '16 at 11:33
  • @user11230 If you reopen your app you can retrieve it in onCreate, but if you go another activity and change your list then you can add your method to onResume – Yasin Kaçmaz Jul 09 '16 at 11:35
  • Sorry to bother you again but I can't figure out the varibles he used in the code. What are myScores, scoreEditor? – user11230 Jul 10 '16 at 00:14
  • @user11230 Never mind my friend scoreEditor is a `SharedPreferences.Editor` and myScores is a `SharedPreferences`. – Yasin Kaçmaz Jul 10 '16 at 00:56
  • Solved it, I will now add my own answer. Thanks mate. – user11230 Jul 10 '16 at 01:08
0

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();
user11230
  • 539
  • 1
  • 9
  • 19