如何将 List<CustomClass> 转换为 ArrayList<CustomClass>,反之亦然?

How to convert List<CustomClass> to ArrayList<CustomClass> and vise-versa?

我有一个 CustomClass,它是 Bean/Model/Pojo,它用于保存来自 Json 的自定义对象。我想将我的 CustomClassArrayList 保存在 SharedPreference 中,并在特殊情况下将其取回。我找到了一种将它存储在 SharedPreference here 中的方法!正确答案是 SpyZip 我能够存储和检索 List,但我想存储和检索 ArrayList

这是一个片段,展示了我如何在 SharedPreference 中存储和检索值:

    // This method will save custom class ArrayList<Bean>
    public void saveUserFavouriteStations(
            ArrayList<RadioStationBean> radioStation) {
        FavouriteStationHolder = this.getSharedPreferences("stations", 0);
        Editor editor = FavouriteStationHolder.edit();
        Gson gson = new Gson();
        String jsonCars = gson.toJson(radioStation);
        editor.putString("stations", jsonCars);
        System.out.println("Custom ArrayList Saved in App Class");
        editor.commit();
    }

    public ArrayList<RadioStationBean> getUserFavouriteStations() {
        FavouriteStationHolder = this.getSharedPreferences("stations", 0);
        if (FavouriteStationHolder != null) {
            String jsonString = FavouriteStationHolder
                    .getString("stations", "");
            Type type = new TypeToken<List<RadioStationBean>>() {
            }.getType();
            List<RadioStationBean> carsList = gson.fromJson(jsonString, type);
            return carsList;
        } else {

            return null;
        }
    } 

ArrayList is a specific (array based) implementation of the List界面。由于您应该很少关心底层实现,而且这似乎不是那些罕见的情况之一,只需更改您的方法签名以在参数列表和 return 值中使用 List .

您只需将 java 对象转换为字符串并将此字符串存储在 SharedPreferences 中,而在检索数据时您正在将此字符串转换回 java 对象。使用类型 ArrayList 获取 ArrayList 中的数据。

  String jsonString = FavouriteStationHolder
                .getString("stations", "");
        Type type = new TypeToken<ArrayList<RadioStationBean>>() {
        }.getType();
        ArrayList<RadioStationBean> carsList = gson.fromJson(jsonString, type);

遗憾但 none 解决方案有效,A List<CustomClass> 不能直接转换为 ArrayList<CustomClass> 因此,我尝试通过解析 json i 来制定自己的解决方案通过创建一个新的 CustomClass 对象并使用 for 迭代器从 gson 获取并使用以下代码

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