我希望以下代码将一些数据保存到 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();
}
});
由于某种原因,它不起作用。我确实在其他地方引用了该文件,但它似乎在这些区域工作正常。