如何将List <custom class>转换为ArrayList <custom class>,反之亦然?</customclass> </customclass>

时间:2015-01-28 07:09:23

标签: java android list arraylist sharedpreferences

我有CustomClass Bean/Model/Pojo,它用于保存Json的自定义Obejcts。我想在ArrayList中保存CustomClass的{​​{1}}并在特殊情况下将其恢复。我找到了一种将其存储在SharedPreference here中的方法!正确的答案是SharedPreference 我可以存储和检索SpyZip,但我想存储和检索List

以下是一个代码段,展示了如何在ArrayList中存储和检索值:

SharedPreference

3 个答案:

答案 0 :(得分:6)

ArrayListList接口的特定(基于数组)实现。由于您应该很少关心底层实现,并且这似乎不是极少数情况之一,只需更改方法签名以在参数列表和返回值中使用List。 / p>

答案 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迭代器使用以下代码

构造新的ArrayList
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