如何重命名的Andr​​oid现有的共享preferences文件 [英] How to rename an existing shared preferences file in Android

查看:99
本文介绍了如何重命名的Andr​​oid现有的共享preferences文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的机器人。我已创建的共享preferences存储播放列表名称,并在播放列表中的歌曲名称。现在我要重新命名播放列表。

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.

和另外一个是:我如何删除共享preferences文件(即 PlaylistName.xml ),当我删除播放列表

And another is: How do I delete the SharedPreferences file (i.e PlaylistName.xml), when I delete the playlist?

推荐答案

最后,我能够重新命名的共享preference文件。

At last, I am able to rename the sharedpreference file.

有关参考,在我的背景下,code是:

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;

和删除 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();

这篇关于如何重命名的Andr​​oid现有的共享preferences文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆