3

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?

sth
  • 222,467
  • 53
  • 283
  • 367
Babasaheb
  • 683
  • 6
  • 20

3 Answers3

6

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(); 
Ashraf Sousa
  • 246
  • 3
  • 4
6

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();
sth
  • 222,467
  • 53
  • 283
  • 367
Babasaheb
  • 683
  • 6
  • 20
  • oops! code is disturbed! Please, try to understand. I am new to the site and android also Thanks Vladimir Ivanov for suggestions. – Babasaheb May 02 '11 at 08:51
  • you might want to up-vote his answer, then, if it helped. I've also submitted an edit for your answer to help clean up the formatting. – James Skemp May 02 '11 at 12:15
1

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.

Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
  • Thanks Vladimir. Ok, I will use database. For deleting sp file, I used 'deleteFile(fileName);' It does not delete the file. – Babasaheb Apr 28 '11 at 06:40