theForgottenCoder
theForgottenCoder

Reputation: 145

How do i save a arraylist in android?

I am trying to store an ArrayList in onstop() and recover them by calling getlist() on startup but it doesn't seem to work: nothing gets recovered and not sure if its even storing is my code correct?

public class Schedule extends Activity {
Context context = this;
ArrayList<appointment> listoffappointments = new ArrayList<appointment>();
ArrayList<appointment> myArray;
ArrayList<Calendar> listofcalenders = new ArrayList<Calendar>();
 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
setContentView(R.layout.activity_appointments);
instance =this;
listView = (ListView) findViewById(R.id.myDataListListView) ;     
    refresh();
}

   public void refresh()
    {

        adapter = new ArrayAdapter(instance,R.layout.mylist, listoffappointments) ;

        listView.setAdapter(adapter) ;
    }


@overide
protected void onResume() {

    FileInputStream input = null;
    ObjectInputStream inputob = null;
    try {
        input = openFileInput("app.ser");
        inputob = new ObjectInputStream(input);
         myArray = (ArrayList<appointment>) inputob.readObject(); 




 // listoffappointments.addAll(myArray);
 THIS IS WHERE I NEED HELP I WANT THE INITIAL LIST LISTOFFAPPOINTMENTS TO HAVE ALL THE ELEMENTS OF myArray when i print each element in myArray onclose the elements i add are present but when i do listoffappointments.addall(myArray); i get a null pointerexception this is why i have the code commented


        inputob.close();
        input.close();


    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    super.onResume();
}


@Override
protected void onStop() {



    try {
        FileOutputStream file  =openFileOutput("app.ser", Context.MODE_PRIVATE);
        ObjectOutputStream oos= new ObjectOutputStream(file);
        oos.writeObject(listoffappointments);
        oos.close();
        file.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    for(appointment b : myArray)
    {
        System.out.println(b.gettext());
    }


    super.onStop();
}

please take a look at the comment in my code for onresume()

Upvotes: 0

Views: 282

Answers (1)

Justin Pollard
Justin Pollard

Reputation: 6679

First, I think you should add a serialVersionUID to your Serializable object. This is good practice for future compatibility.

private static final long serialVersionUID = 1l;

Second, you probably want to capitalize you object name (Appointment); that's the convention.

Third, you should read/write your object from/to your app's file directory. You get that directory using Context.getFilesDir(). Here's an example of what the code might look like:

private List<Appointment> mAppointments = new ArrayList<Appointment>();
private List<Appointment> mListOfAppointments = new ArrayList<Appointment>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mFile = new File(getActivity().getFilesDir(), "MyFile.ser");

}


@Override
public void onResume() {
    super.onResume();

    try {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(mFile));
        mAppointments = (List<Appointment>) ois.readObject();
        ois.close();

        if(mAppointments != null && !mAppointments.isEmpty()){
            mListOfAppointments.clear();
            mListOfAppointments.addAll(mAppointments);
        }

        Log.i(TAG, mAppointments.toString());
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}


@Override
public void onStop() {
    super.onStop();

    try {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(mFile));

        List<Appointment> list = new ArrayList<Appointment>();
        Appointment appointment = new Appointment(Calendar.getInstance(), "The Calendar");
        list.add(appointment);

        oos.writeObject(list);
        oos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

The code above writes a List<Appointment> to the file MyFile.ser in onStop(). In onResume(), it reads that List back into memory.

MAKE SURE you check, in onResume(), that mAppointments IS NOT NULL because it will be null the first time you open your app (simply because you haven't created "app.ser" yet - you don't crate it til the first onStop()). Your list of listoffappointments will be empty the first time, but during subsequent app openings, provided the appointment list wasn't null during the first onStop(), onResume() will find the file and populate the list of appointments appropriately.

Upvotes: 1

Related Questions