-2

I have created a ArrayList, which contains Object of a class. After complete of inserting all of the objects into that array list how to push that array list into a Shared Preference ?

The Model class:

public class Weather {
    private double minTemp, maxTemp, humidity, windSpeed;
    private String date, desc;

    public String getDate() {
        return date;
    }
    public void setDate(String date) {
        this.date = date;
    }

    public String getDesc() {
        return desc;
    }
    public void setDesc(String desc) {
        this.desc = desc;
    }

    public double getMinTemp() {
        return minTemp;
    }
    public void setMinTemp(double minTemp) {
        this.minTemp = minTemp;
    }

    public double getMaxTemp() {
        return maxTemp;
    }
    public void setMaxTemp(double maxTemp) {
        this.maxTemp = maxTemp;
    }

    public double getHumidity() {
        return humidity;
    }
    public void setHumidity(double humidity) {
        this.humidity = humidity;
    }

    public double getWindSpeed() {
        return windSpeed;
    }
    public void setWindSpeed(double windSpeed) {
        this.windSpeed = windSpeed;
    }
}

ArrayList Creation:

ArrayList<Weather> weatherArr = new ArrayList<Weather>();

for(....){
  Weather weather = new Weather();
  weather.setDate(date);
  weather.setDesc(description);
  weather.setMinTemp(minTemp);
  weather.setMaxTemp(maxTemp);
  weather.setHumidity(humidity);
  weather.setWindSpeed(windSpeed);
  weatherArr.add(weather);
}

My Shared Preference:

SharedPreferences weatherSharedpreferences = weatherSharedpreferences = getSharedPreferences(WeatherPREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = weatherSharedpreferences.edit();

Now How to insert this ArrayList(weatherArr) into the SharedPreferences(weatherSharedpreferences)?

Saibal
  • 1
  • 2
  • I found something here, but it might be trickier than using 3rd party libs as in answer: [Serializable Array](http://stackoverflow.com/questions/14705860/saving-serializable-objects-list-into-sharedpreferences) – David Kasabji Jan 12 '17 at 12:58
  • You cannot. Shared prefs is for primitive data types: https://developer.android.com/guide/topics/data/data-storage.html#pref Consider a database or file storage. – James Jan 12 '17 at 12:59
  • https://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html go to this link.. – Sneha Sarkar Jan 12 '17 at 13:01

1 Answers1

4

I have a trick for you. Add Gson to your dependencies.

repositories {
    mavenCentral()
}

dependencies {
    compile 'com.google.code.gson:gson:2.2.4'
}

Convert your object to Json and save to SharedPrefs as a String.

String data = new Gson().toJson(weatherArr)

Convert it back from String when you need it.

List<Weather> data = new Gson(stringData, new TypeToken<List<Weather>>(){}.getType()).
Andrej Jurkin
  • 2,216
  • 10
  • 19
  • As you convert the ArrayList of object Weather into a String named "data", then I need the ArrayList of object Weather back from that string not a single Weather class. how to do that ? – Saibal Jan 12 '17 at 14:02
  • you can use TypeToken - new TypeToken>(){}.getType() – Andrej Jurkin Jan 12 '17 at 14:07