2015-06-22 53 views
0

我有一個HashMap,其中包含與特定活動相關的數據。問題是,當應用程序閒置一段時間後打開時,HashMap是空的。我正在獲取主要活動中的所有數據,並將相應的對象存儲在自定義類中的HashMap中。除了有些時候應用程序閒置了很長時間之後,一切都運行良好,然後HashMap爲所有鍵返回空值。只要有我的應用程序正在運行的實例,將數據保存在HashMap中的正確方法是什麼?應用程序空閒時使用對象數據保持HashMap - Android

public class ApplicationCache { 

    private static ApplicationCache cache; 
    private static HashMap<String, Object> map; 

    private ApplicationCache(){ 
     map = new HashMap<String, Object>(); 
    } 

    public static ApplicationCache getInstance(){ 
     if(cache == null){ 
      cache = new ApplicationCache(); 
     } 
     return cache; 
    } 

    public void setValue(String name, Object value){ 
     map.put(name, value); 
    } 

    public Object getValue(String name){ 
     return map.get(name); 
    } 

    public void removeValue(String name){ 
     map.remove(name); 
    } 

    public void clear(){ 
     map.clear(); 
    } 

} 

我存儲對象爲從一個ListView中的主要活動中單擊事件的HashMap:

ApplicationCache.getInstance().setValue("details", bankList.get(position)); 

在另一個活動我是從這個HashMap的讀取值:

details = (DetailsVO) ApplicationCache.getInstance().getValue("details"); 
+0

是否有可能通過你HashMap對象存儲SharedPreference? \ –

+0

首選項只接受原始類型,所以你不能把一個複雜的對象在其中 – Deepak

+0

@Deepak使HashMap的靜態 –

回答

0

可能是再次加載的活動是onCreate()被調用。因此保存狀態並進行恢復。使用onSaveInstanceState()和onRestoreInstanceState()。讓我知道它是否有效。

+0

如何o將具有對象數據的hasmap存儲到onSaveInstanceState()和onRestoreInstanceState()中。請幫助我。我是Android的初學者。 – Deepak

+0

你在散列表中存儲什麼?如果你只是存儲字符串然後使用JSON你的問題可能會得到解決。 –

+0

沒有..我存儲自定義對象與哈希值中的值.. – Deepak

0

我寫了一段簡單的代碼來優先保存地圖,並從首選項加載地圖。不需要GSON功能。我剛剛使用了一個具有String作爲鍵和布爾值作爲值的映射。

private void saveMap(Map<String,Boolean> inputMap){ 
      SharedPreferences pSharedPref = getApplicationContext().getSharedPreferences("MyVariables", Context.MODE_PRIVATE); 
      if (pSharedPref != null){ 
       JSONObject jsonObject = new JSONObject(inputMap); 
       String jsonString = jsonObject.toString(); 
       Editor editor = pSharedPref.edit(); 
       editor.remove("My_map").commit(); 
       editor.putString("My_map", jsonString); 
       editor.commit(); 
      } 
     } 

private Map<String,Boolean> loadMap(){ 
      Map<String,Boolean> outputMap = new HashMap<String,Boolean>(); 
      SharedPreferences pSharedPref = getApplicationContext().getSharedPreferences("MyVariables", Context.MODE_PRIVATE); 
      try{ 
       if (pSharedPref != null){  
        String jsonString = pSharedPref.getString("My_map", (new JSONObject()).toString()); 
        JSONObject jsonObject = new JSONObject(jsonString); 
        Iterator<String> keysItr = jsonObject.keys(); 
        while(keysItr.hasNext()) { 
          String key = keysItr.next(); 
          Boolean value = (Boolean) jsonObject.get(key); 
          outputMap.put(key, value); 
         } 
       } 
      }catch(Exception e){ 
       e.printStackTrace(); 
      } 
      return outputMap; 
     } 
+0

我的工作,它的工作對我來說請chech天氣它的工作你還是不是。 –

+0

首先告訴我它是否適合您或您的情況? –

0

我寫了這個簡單的方法來存儲在內部文件存儲的對象,你可以使用它,只是烏爾的Hashmap取代的JSONObject

public static void writeToCache(String fileName, JSONObject jObject) { 
      ObjectOutput out; 
      try { 
       if(!(new File(BASE_PATH)).exists()){ 
        new File(BASE_PATH).mkdirs(); 
       } 
       out = new ObjectOutputStream(new FileOutputStream(
         new File(BASE_PATH+fileName))); 
       out.writeObject(jObject.toString()); 
       out.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } 


public static JSONObject getJsonObject(String fileName) { 
     ObjectInputStream in; 
     JSONObject jsonObject = null; 
     try { 
      in = new ObjectInputStream(new FileInputStream(new File(BASE_PATH+fileName))); 
      jsonObject = new JSONObject(in.readObject().toString()); 
      in.close(); 
     } catch (IOException | ClassNotFoundException | JSONException e) { 
      // TODO Auto-generated catch block 
      //e.printStackTrace(); 
      return jsonObject; 
     } 
     return jsonObject; 
    } 
+0

感謝您的回覆..如何將hasmap與對象數據一起存儲到onSaveInstanceState()和onRestoreInstanceState()..請幫助我。我是Android的初學者。 – Deepak

+0

onSaveInstanceState使用putSerializable(key,Hashmap)方法將你的hashmap放在Bundle對象中,並且你在onCreate()方法中得到該Bundle – NullByte

+0

好吧,我會盡力去做。 – Deepak

相關問題