我使用以下代码将曲目添加到收藏夹
private void addToFavourites(LocalTrack localTrack) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();
Gson gson = new Gson();
int imgKey = 0;
String json = gson.toJson(localTrack);
editor.putString("" + imgKey, json);
editor.apply();
imgKey++;
}
我使用以下代码从收藏中删除曲目。
private void removeFromFavourites(LocalTrack localTrack){
ArrayList<LocalTrack> localTracks = loadPreferences();
for (int i = 0; i < localTracks.size(); i++){
if (localTracks.get(i).getPath().equals(localTrack.getPath())){
localTracks.remove(localTrack);
}
}
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();
Gson gson = new Gson();
int imgKey = 0;
String json = gson.toJson(localTracks);
editor.putString("" + imgKey, json);
editor.apply();
imgKey++;
}
向收藏夹添加曲目非常有效。 从收藏夹删除轨道错误地对齐json,因此我得到如下错误,
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
如何在没有jsonsyntax异常的情况下从json中删除轨道?
更新
public static ArrayList<LocalTrack> loadPreferences() {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.act);
Gson gson = new Gson();
Map<String, ?> allPrefs = sharedPreferences.getAll();
String gsonString = sharedPreferences.getString("userImages", "");
ArrayList<LocalTrack> images = new ArrayList<LocalTrack>();
if (!allPrefs.isEmpty()) {
for (Map.Entry<String, ?> entry : allPrefs.entrySet()) {
String json = entry.getValue().toString();
LocalTrack temp = gson.fromJson(json, LocalTrack.class);
images.add(temp);
}
}
return images;
}
我添加了加载首选项方法
答案 0 :(得分:0)
你可以直接反序列化通用集合:
Sub TestReplace()
ReplaceSheetNameInFormula Range("A1:C6"), "A", "Print"
ReplaceSheetNameInFormula Range("A1:C6"), "A-b", "Print"
End Sub
Sub ReplaceSheetNameInFormula(Target As Range, OldSheet As String, NewSheet As String)
Const TempName = "TempDefinedName"
Dim What As String, Replacement As String
Replacement = "'" & NewSheet & "'!"
With Worksheets(NewSheet)
.Names.Add TempName, Worksheets(OldSheet).Range("A1"), False
What = Mid(.Names(TempName), 2, Len(.Names(TempName)) - 5)
End With
Target.Replace What, Replacement
End Sub