2013-05-02 55 views
0

的我想下面的代碼一些數據保存到一個文件SharedPreferences:無法設定SharedPreferences,不知道問題

 // Assign external resources 
     SaveButton = (Button)findViewById(R.id.button6);  

     // Set the listener for the interact button 
     SaveButton.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 


       SharedPreferences allMaps = getSharedPreferences("allMaps", Context.MODE_PRIVATE); 
       SharedPreferences.Editor editor = allMaps.edit(); 

       SharedPreferences currentMap = getSharedPreferences("currentMap", Context.MODE_PRIVATE); 

       String currentMapName = currentMap.getString("MapName", ""); 

       Map<String,?> keys = allMaps.getAll(); 
       for(Map.Entry<String,?> entry : keys.entrySet()){ 
        //delete objects with currentMap MapName 
        if (entry.getKey().toString().contains(currentMapName)) { 
         editor.remove(entry.getKey().toString()); 
        } 
       } 
       //put mapname here 
       editor.putString("MapName" + currentMapName, currentMapName); 

       //put objects from arrays into allMaps 
       //need reference of mindMapView view object 
       MindMapView mindMapView = (MindMapView)findViewById(R.id.MindMapView); 

       for (int i = 0; i < mindMapView.mindMapArrayListLabels.size(); i++) { 
        editor.putString(currentMapName + "Label" + i, mindMapView.mindMapArrayListLabels.get(i)); 
       } 
       for (int i = 0; i < mindMapView.mindMapArrayListImgs.size(); i++) { 
        editor.putInt(currentMapName + "Image" + i, mindMapView.mindMapArrayListImgs.get(i)); 
       } 

       editor.commit(); 
      } 

     }); 

出於某種原因,它不工作。我在其他地方參考了這個文件,但它似乎在這些領域工作得很好。

回答

0

你可以在this答案中使用PublicSharedPreferences類。它對我來說非常合適。希望這可以幫助。