2012-10-29 37 views
0

我目前正在研究一種Android應用程序,它將有點像學生計劃者。我的後端是所有的Java和我目前卡住,因爲我存儲我從我的後端創建的對象arraylist。作爲java,只要程序終止,這些對象就會消失。下次啓動我的應用程序時,我可以存儲我的Java對象以進行檢索的最簡單方法是什麼?任何幫助深表感謝!我開發2.3和日食(juno)。在Android中存儲用於檢索的數組列表

回答

0

您可以使用Shared Preferences來存儲您的數據。 SharedPreferences可讓您在應用程序中保留原始數據類型的鍵值對。您無法使用單個密鑰存儲整個ArrayList,但可以遍歷數組併爲列表中的每個值系統生成一個密鑰。我通常做這樣的事情:

public class SomeActivity extends Activity { 
    private ArrayList<Data> list; //We'll persist this array 

    /* snip */ 

    //These strings can be anything - you just need something you can use to systematically generate distinct keys 
    public static final String LIST_KEY = "SomeActivity_List"; 
    public static final String LIST_LENGTH_KEY = "SomeActivity_ListLength"; 

    /** 
    * How this method works: It starts by getting a SharedPreferences object, 
    * which offers an API for persisting data. It then systematically generates 
    * Strings like "SomeActivity_List1", "SomeActivity_List2", "SomeActivity_List3", 
    * and so on to use as keys fot the 1st, 2nd, 3rd, etc. elements in the list. Then 
    * it Data.saveData(), a method defined below in the Data class, in order to give 
    * each Data object in the ArrayList an opportunity to persist its primitive 
    * members in the SharedPreferences. 
    * 
    * SomeActivity.restoreList() works similarly. 
    */ 
    public void saveList() { 
     SharedPreferences prefs = getPreferences(); //This method is part of the Activity class 
     SharedPreferences.Editor editor = prefs.getEditor(); 

     //Save the length of the list so that when we restore it, we know how many 
     //Data objects to recreate. 
     editor.putInt(LIST_LENGTH_KEY, list.size()); 
     editor.commit(); 

     //This for loop is important - note how we concatenate i to each of the keys to give each element in list a distinct key 
     for(int i = 0; i < list.size(); i++) 
     { 
      String identifier = LIST_KEY + Integer.toString(i); //generate distinct keys 
      list.get(i).saveData(identifier, prefs); 
     } 
    } 

    public void restoreList() { 
     SharedPreferences prefs = getPreferences(); 

     int length = prefs.getInt(LIST_LENGTH_KEY); 

     for(int i = 0; i < length; i++) 
     { 
      String identifier = LIST_KEY + Integer.toString(i); //re-generate distinct keys 
      Data data = new Data(); 
      data.restoreData(identifier, prefs); 
      list.addLast(data); 
     } 
    } 

    public static class Data { 
     private int i; 
     private double j; 
     private String s; 

     public static final String I_KEY = "Data_I" 
     public static final String J_KEY = "Data_J" //strings can really be whatever, as long as they're distinct. 
     public static final String S_KEY = "Data_K" 

     /** 
     * How this method works: The SomeActivity.saveList() method generates a 
     * unique String ("identifier") for each of the Data objects it contains. 
     * This method uses that distinct string and makes some more distinct keys 
     * to store each of Data's primitive members. 
     * 
     * restoreData() works similarly when it rebuilds Data objects 
     */ 
     public saveData(String identifier, SharedPreferences prefs) { 
      SharedPreferences.Editor editor = prefs.getEditor(); 
      editor.putInt(I_KEY + identifier, i); 
      editor.putDouble(J_KEY + identifier, j); 
      editor.putString(S_KEY + identifier, s); 
      editor.commit(); 
     } 

     public restoreData(String identifier, SharedPreferences prefs) { 
      i = prefs.getInt(I_KEY + identifier); 
      j = prefs.getDouble(J_KEY + identifier); 
      s = prefs.getString(S_KEY + identifier); 
     } 
    } 
} 

這種方法遞歸地工作。例如,如果Data有一個ArrayList作爲它的一個字段,那麼它可以系統地將該列表中的所有值存儲在SharedPreferences中。如果用戶卸載您的應用程序或清除應用程序數據,則存儲的列表將被刪除。根據數據的性質,您可能會或可能不會想要這種行爲。

相關問題