问题
I am new to android. I have created SharedPreferences to store a playlist name and song names in the playlist. Now I have to rename the playlist.
And another is: How do I delete the SharedPreferences file (i.e PlaylistName.xml
), when I delete the playlist?
回答1:
At last, I am able to rename the sharedpreference file.
For reference, in my context the code is:
String fileName=etlistName.getText().toString();
File f=new File("/data/data/eywa.musicplayer/shared_prefs/"+PlayListName+".xml");
f.renameTo(new File("/data/data/eywa.musicplayer/shared_prefs/"+fileName+".xml"));
SharedPreferences mySharedPreferences=getSharedPreferences("list_of_playlist",Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = mySharedPreferences.edit();
editor.remove(PlayListName);
editor.putString(fileName, fileName);
editor.commit();
PlayListName=fileName;
And for deleting playlistName.xml
:
for (int i=0; i<selectedItems.size();i++)
{//remove the songs names from the playlist
SharedPreferences sp=getSharedPreferences(selectedItems.get(i),Activity.MODE_PRIVATE);
SharedPreferences.Editor ed=sp.edit();
ed.clear();
ed.commit();
//remove the play list name from the list_of_playlist
SharedPreferences.Editor editor = mainPref.edit();
editor.remove(selectedItems.get(i));
//delete .xml file
File f=new File("/data/data/eywa.musicplayer/shared_prefs/"+selectedItems.get(i)+".xml");
if(f.delete())
System.out.println("file deleted")
editor.commit();
}
selectedItems.clear();
回答2:
The file access from "/data/data/..." is not reliable since I think it is not the same path for all the phones (Samsung devices namely are different AFAIK).
I prefer the following method which basically 'replicates' the old shared prefs and then clears it. This method does not remove the old shared prefs file itself but more relibale IMHO.
SharedPreferences settingsOld = context.getSharedPreferences(nameOld, Context.MODE_PRIVATE);
SharedPreferences settingsNew = context.getSharedPreferences(nameNew, Context.MODE_PRIVATE);
SharedPreferences.Editor editorNew = settingsNew.edit();
Map<String, ?> all = settingsOld.getAll();
for (Entry<String, ?> x : all.entrySet()) {
if (x.getValue().getClass().equals(Boolean.class)) editorNew.putBoolean(x.getKey(), (Boolean)x.getValue());
else if (x.getValue().getClass().equals(Float.class)) editorNew.putFloat(x.getKey(), (Float)x.getValue());
else if (x.getValue().getClass().equals(Integer.class)) editorNew.putInt(x.getKey(), (Integer)x.getValue());
else if (x.getValue().getClass().equals(Long.class)) editorNew.putLong(x.getKey(), (Long)x.getValue());
else if (x.getValue().getClass().equals(String.class)) editorNew.putString(x.getKey(), (String)x.getValue());
}
editorNew.commit();
SharedPreferences.Editor editorOld = settingsOld.edit();
editorOld.clear();
editorOld.commit();
回答3:
You have chosen not the best storage for playlists. Database much more suits your needs. Although, you can still delete the sp file using basic java io.
来源:https://stackoverflow.com/questions/5788109/how-to-rename-an-existing-shared-preferences-file-in-android