-2
這是我嘗試插入數據以共享首選項時我點擊Item的代碼。Android - 我嘗試從SharePreference獲取數據
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.popup_Favorite:
songList = songRespones.getSongs();
SharedPreferences sharedPreferences = getContext().getSharedPreferences(
SharePreferenceKey.SONG_LIST, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
String songJson = new Gson().toJson(songList.get(position));
editor.putString(SharePreferenceKey.SONG_LIST, songJson);
editor.apply();
showMessage("អ្នកបានរក្សាទុក");
break;
case R.id.popup_cencel:
showMessage("Cancel");
break;
}
return false;
}
});
這裏是我的代碼,我想從分享偏好
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
SharedPreferences sharedPreferences = getContext().getSharedPreferences(
SharePreferenceKey.SONG_LIST, Context.MODE_PRIVATE);
String getSongJson = sharedPreferences.getString(SharePreferenceKey.SONG_LIST, "N/A");
List<SongRespones> songList;
if (!getSongJson.equals("N/A")) {
Type type = new TypeToken<List<SongRespones>>(){}.getType();
songList = new Gson().fromJson(getSongJson, type);
Log.e("ppppp", songList.size() + "");
}
}
獲取數據,但我不能得到它。它顯示以下錯誤:
FATAL EXCEPTION: main Process: com.vichit.khmersong, PID: 20040
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException:
Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
at com.google.gson.Gson.fromJson(Gson.java:899)
at com.google.gson.Gson.fromJson(Gson.java:852)
at com.google.gson.Gson.fromJson(Gson.java:801)
請幫幫我:
我想按照我點擊的位置分享偏好數據。 – Vichit
你在'songList.get(position)'中的位置如何? –
當我點擊recycleview並且想將點擊的數據放入共享首選項時,我有位置。 – Vichit