2014-04-04 64 views
0

我試圖存儲在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()

+0

你的logcat絕對值得一讀。 EOFException應該是您的主要焦點:p – keyser

+0

我建議您查看ORMLite。它對於對象集合的存儲非常有效。 http://ormlite.com/javadoc/ormlite-core/doc-files/ormlite_4.html#Use-With-Android在我的GetList方法 –

+0

我做的ArrayList myArray的=(ArrayList的)inputob.readObject(); inputob.readObject();那是我的錯誤 – theForgottenCoder

回答

1

首先,我認爲你應該添加一個serialVersionUIDSerializable對象。這是未來兼容性的良好做法。第二,你可能想要大寫你的對象名(Appointment);或者你可能想要大寫你的對象名(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(); 
    } 
} 

上面的代碼中onStop()寫入List<Appointment>到文件MyFile.ser。在onResume()中,它將List讀回到內存中。

請確認您檢查,在onResume(),那mAppointmentsIS NOT NULL因爲這將是空你第一次打開你的應用程序(只是因爲你還沒有創建「app.ser」,但 - 你不直到第一個onStop())。你的listoffappointments列表是空的第一次,但在隨後的應用開口,所提供的第一onStop()onResume()在約會列表不是空會發現文件和適當填充任命名單。

+0

喜賈斯汀的原因非常感謝你爲這個進一步尋找到我的代碼後,所以我已經發現,ArrayList中導致基於關閉你的代碼從一個要素轉移到另一個例如當一個強制關閉你的onresume()當你得到mAppointments =(列表)ois.readObject();我把它分配給我已經分配給我的代碼像Arraylist globalist = new Arraylist ();然後mAppointments將具有所有的價值,但在調用globallist.addall(mAppointments);我收到一個關閉的力量 – theForgottenCoder

+0

你可以發佈你所有相關部分的代碼嗎?也許有任何相關的堆棧跟蹤? –

+0

賈斯汀·波拉德我添加的代碼的多個部分,請查閱的onResume()方法,我已經寫了我的問題的意見更加具體的感謝 – theForgottenCoder

相關問題