私はアンドロイドを初めて使っています。プレイリストにプレイリスト名と曲名を格納するSharedPreferencesを作成しました。今私はプレイリストの名前を変更する必要があります。Androidで既存の共有設定ファイルの名前を変更するには
もう1つです:プレイリストを削除すると、SharedPreferencesファイル(つまりPlaylistName.xml
)を削除するにはどうすればよいですか?
私はアンドロイドを初めて使っています。プレイリストにプレイリスト名と曲名を格納するSharedPreferencesを作成しました。今私はプレイリストの名前を変更する必要があります。Androidで既存の共有設定ファイルの名前を変更するには
もう1つです:プレイリストを削除すると、SharedPreferencesファイル(つまりPlaylistName.xml
)を削除するにはどうすればよいですか?
最後に、sharedpreferenceファイルの名前を変更できました。
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();
プレイリストに最適なストレージを選択していません。データベースはあなたのニーズにはるかに適しています。 基本的なjava ioを使用してspファイルを削除することはできますが、
ありがとうVladimir。さて、私はデータベースを使用します。 spファイルを削除するために、私は 'deleteFile(fileName);'を使用しました。ファイルは削除されません。 – Baba
"/データ/データ/ ..." からのファイルアクセスが信頼できない私のコンテキストでコードが参考
、私はそれがすべての携帯電話(サムスンのデバイスは、すなわち別のAFAIK)のための同じパスではないと思うから。
私は、基本的に古い共有設定を '複製'して消去する以下の方法を好みます。この方法では古い共有設定ファイル自体は削除されませんが、より多くの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();
おっと!コードが乱れる! ご理解ください。私はサイトやアンドロイドにも新しいです おかげでウラジミールイワノフに提案してください。 – Baba
あなたが彼の答えを選ぶことができたら助けてください。また、書式を整理するための回答を編集して提出しました。 –