我有CustomClass
Bean/Model/Pojo
,它用于保存Json的自定义Obejcts。我想在ArrayList
中保存CustomClass
的{{1}}并在特殊情况下将其恢复。我找到了一种将其存储在SharedPreference
here中的方法!正确的答案是SharedPreference
我可以存储和检索SpyZip
,但我想存储和检索List
。
以下是一个代码段,展示了如何在ArrayList
中存储和检索值:
SharedPreference
答案 0 :(得分:6)
答案 1 :(得分:1)
您只需将java对象转换为字符串并将此字符串存储在SharedPreferences中,并在检索数据时将此String转换回java Object。使用Type ArrayList获取ArrayList中的数据。
String jsonString = FavouriteStationHolder
.getString("stations", "");
Type type = new TypeToken<ArrayList<RadioStationBean>>() {
}.getType();
ArrayList<RadioStationBean> carsList = gson.fromJson(jsonString, type);
答案 2 :(得分:1)
很遗憾,但没有解决方案有效,A List<CustomClass>
无法直接转换为ArrayList<CustomClass>
所以,我试图通过解析我从gson获得的json来制作一个新的CustomClass对象来制作我自己的解决方案使用for
迭代器使用以下代码
if (FavouriteStationHolder != null) {
String jsonString = FavouriteStationHolder
.getString("stations", "");
System.out.println("json String got in App class-->" + jsonString);
Type type = new TypeToken<List<RadioStationBean>>() {
}.getType();
List<RadioStationBean> stations = gson.fromJson(jsonString, type);
ArrayList<RadioStationBean> stationsList = new ArrayList<RadioStationBean>();
for (int i = 0; i < stations.size(); i++) {
RadioStationBean rBean = new RadioStationBean();
rBean.setAdvertisements(stations.get(i).getAdvertisements());
rBean.setCategories_id(stations.get(i).getCategories_id());
rBean.setCategoy(stations.get(i).getCategoy());
rBean.setDescription(stations.get(i).getDescription());
rBean.setFacebook(stations.get(i).getFacebook());
rBean.setId(stations.get(i).getId());
rBean.setImage_url(stations.get(i).getImage_url());
rBean.setIsDispTrack_Artist(stations.get(i)
.getIsDispTrack_Artist());
rBean.setManager_detail(stations.get(i).getManager_detail());
rBean.setPhone_show(stations.get(i).getPhone_show());
rBean.setPhone_studio(stations.get(i).getPhone_studio());
rBean.setPhone_toll_free(stations.get(i).getPhone_toll_free());
rBean.setPlatform(stations.get(i).getPlatform());
rBean.setStation_image(stations.get(i).getStation_image());
rBean.setStation_manager(stations.get(i).getStation_manager());
rBean.setStation_manager_id(stations.get(i)
.getStation_manager_id());
rBean.setStation_name(stations.get(i).getStation_name());
rBean.setStation_status(stations.get(i).getStation_status());
rBean.setStream_format(stations.get(i).getStream_format());
rBean.setStream_url(stations.get(i).getStream_url());
rBean.setTheme_color(stations.get(i).getTheme_color());
rBean.setTwitter(stations.get(i).getTwitter());
rBean.setWeb_portal(stations.get(i).getWeb_portal());
stationsList.add(rBean);
}
System.out.println("Custom ArrayList Retrieved in App Class"
+ stationsList.get(0).getDescription());
return stationsList;
}
非常感谢所有人努力帮助我并开始使用Downvoter和Duplicators