3

Oh boy, so I've spent the last hour or so reading and trying different ways of storing an ArrayList of a Parcelable Object in the Android preferences and Local Storage, and now I'm back to square one. I have downloaded and imported Gson, by the way, not that I could get it to work.

Basically, I have a class called Task in an ArrayList<> called taskList that I need to save even when my users close my app. The Object consists of a String, some Ints, some Booleans and an ArrayList of class Subtask if that matters. What's important is that it seems I can't just write it as a list of Strings, and all the tutorials I've found only show saving simple ArrayLists like Strings and they have all been Serializable, not Parcelable.

EDIT: thanks for the suggestions, but I implement Parcelable to parcel specific objects in the list between activities..

  • Where do you want to store it? In a file or SharedPreferences? Either way have a look at this: http://stackoverflow.com/questions/7057845/save-arraylist-to-sharedpreferences – vkislicins Apr 15 '15 at 11:11
  • @vkislicins I'd love to save it in SharedPreferences, as that seems simpler, but it looks like it has to be Serializable and couldn't save all my values.. – Oliver G Hjermitslev Apr 15 '15 at 11:12
  • Parcelable is explicitly NOT intended for persistent storage. You are better of using Serializable, or implementing your own storage schema. – JHH Apr 15 '15 at 11:13
  • The answer in the link above should help you – vkislicins Apr 15 '15 at 11:13
  • I would suggest not to save a bunch of data into the (Shared)-Preferences, because as the name implies, they are preferences. I would rather store them as usual into a database or localy. – JacksOnF1re Apr 15 '15 at 11:26

4 Answers4

2

According to Parcelable API,

Parcelable: Interface for classes whose instances can be written to and restored from a Parcel.

And reading in Parcel API:

Parcel is not a general-purpose serialization mechanism. This class (and the corresponding Parcelable API for placing arbitrary objects into a Parcel) is designed as a high-performance IPC transport. As such, it is not appropriate to place any Parcel data in to persistent storage: changes in the underlying implementation of any of the data in the Parcel can render older data unreadable.

So if you want to store all the references of different types by an ArrayList you must wrap all the objects to a common interface, in this case Serializable is your friend.

Also, according your question:

I need to save even when my users close my app.

If you check the android lifecycle, you will see you need to perform this action in onStop(), which is called when your activity is no longer visible to the user.

android lifecycle

You can also refer to this answer for more information Shared preferences for creating one time activity

Dharman
  • 30,962
  • 25
  • 85
  • 135
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
1

we can save custom array list and get it using SharedPreference

  • For set array list value to SharedPreference
public void SaveArrayList(ArrayList<CustomObj> listArray){
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
        SharedPreferences.Editor editor = prefs.edit();
        Gson gson = new Gson();
        String json = gson.toJson(listArray);
        editor.putString("TAG_LIST", json);  ///"TAG_LIST" is a key must same for getting data 
        editor.apply();     
    }

*For get arraylist from SharedPrefrence

   public ArrayList<CustomObj> getArrayList(){
      SharedPreferences prefs = 
        PreferenceManager.getDefaultSharedPreferences(getContext());
            Gson gson = new Gson();
            String json = prefs.getString("TAG_LIST", null);
            Type listType = new TypeToken<ArrayList<CustomObj>>() {}.getType();
            mSomeArraylist= gson.fromJson(json, listType);
        return gson.fromJson(json, listType);
}
civani mahida
  • 314
  • 3
  • 8
0

You can convert Custom Object to Json format as one string and store in SharedPreference.

Updated

For Example your custom object is Employee

public class Employee
{

    private String name = "test";
    private int age = 29;

    @Override
    public String toString()
    {
        return "Employee [age=" + age + ", name=" + name + "]";
    }
}

In Activity you can convert Java object to String

Gson gson = new Gson();
Employee m_employee = new Employee();
String str = gson.toJson(m_employee);
System.out.println("===== " + str);

You can store str in SharedPreference when user will close application. Get from SharedPreference when user will open application.

0

You could try this:

FileOutputStream fout = new FileOutputStream("c:\\object.ser");
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(yourObject);

For more info, refer this link:- Here