我如何在Android中保存一个arraylist?

时间:2014-04-04 21:55:37

标签: android serialization arraylist

我正在尝试在ArrayList中存储onstop()并在启动时通过调用getlist()来恢复它们但它似乎无法正常工作:没有任何内容可以恢复,也不确定是否它甚至存储我的代码是否正确?

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();
}

请查看我的代码中的注释onresume()

1 个答案:

答案 0 :(得分:1)

首先,我认为您应该在serialVersionUID对象中添加Serializable。这是未来兼容性的良好实践。

private static final long serialVersionUID = 1l;

其次,您可能希望将对象名称(Appointment)大写;这是惯例。

第三,您应该从/向您的应用程序的文件目录读取/写入您的对象。您使用Context.getFilesDir()获取该目录。以下是代码可能的示例:

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();
    }
}

上面的代码将List<Appointment>写入MyFile.ser中的文件onStop()。在onResume()中,它将List读回内存。

确定您在onResume()中检查mAppointments 是非空,因为第一次打开应用时它将为空(只是因为你还没有创建“app.ser” - 你不会把它放到第一个onStop())。您的listoffappointments列表第一次将为空,但在后续应用开放期间,如果在第一个onStop()期间约会列表未为空,onResume()将找到该文件并填充适当的约会清单。