我試圖存儲在onstop()
的ArrayList
並通過調用啓動getlist()
恢復過,但是這似乎並沒有工作:沒有得到恢復,不知道它的存儲,即使是我的代碼是否正確?我如何在android中保存arraylist?
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()
你的logcat絕對值得一讀。 EOFException應該是您的主要焦點:p – keyser
我建議您查看ORMLite。它對於對象集合的存儲非常有效。 http://ormlite.com/javadoc/ormlite-core/doc-files/ormlite_4.html#Use-With-Android在我的GetList方法 –
我做的ArrayList myArray的=(ArrayList的)inputob.readObject(); inputob.readObject();那是我的錯誤 –
theForgottenCoder