-2

I have a CustomClass which is Bean/Model/Pojo and it is used to save Custom Obejcts from Json. I want to save the ArrayList of my CustomClass in SharedPreference and retrieve it back in a special scenario. I found a way of storing it in SharedPreference here! The correct answer is working by SpyZip I am able to store and retrieve a List but I want to store and retrieve an ArrayList instead.

Here is a snippet showing how I store and retrieve value in 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;
        }
    } 
Community
  • 1
  • 1
Bhavik Mehta
  • 573
  • 8
  • 21

3 Answers3

6

ArrayList is a specific (array based) implementation of the List interface. Since you should only rarely care about the underlying implementation, and this doesn't seem to be one of those rare cases, just change your method signatures to use List in both the argument list and the return value.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

You simply converting java object to string and store this string in SharedPreferences and While retrieving data you are converting this String back to java Object. use Type ArrayList to get data in ArrayList.

  String jsonString = FavouriteStationHolder
                .getString("stations", "");
        Type type = new TypeToken<ArrayList<RadioStationBean>>() {
        }.getType();
        ArrayList<RadioStationBean> carsList = gson.fromJson(jsonString, type);
kiran boghra
  • 3,662
  • 2
  • 18
  • 23
1

Sad but none solutions worked, A List<CustomClass> can not be casted directly to ArrayList<CustomClass> so, i tried to make my own solution by parsing the json i get from gson by making a new CustomClass object and using a for iterator to construct a new ArrayList using following code

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;
}

Thanks a lot to all for making efforts to help me and kick off to Downvoter and Duplicators

Bhavik Mehta
  • 573
  • 8
  • 21