Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save Calendar data in shared preferences : Android

I have a ArrayList of Calendar type, And I need to save its data into SharedPreferences. I can save String ArrayList data by using this code:

Set<String> setName = new HashSet<String>();
setName.addAll(ignoredProcesses);
SharedPreferences shrdPref = getSharedPreferences("sharePref", MODE_PRIVATE);
SharedPreferences.Editor prefEditor = shardPref.edit();
prefEditor.putStringSet("keyValue", setName);
prefEditor.commit();

But if I'm following the same approach to save Calendar data, I'm getting error to change Set type to String.

How can I save ArrayList <Calendar> list = new ArrayList<Calendar>(); into SharedPreferences and how I to get it back from SharedPrefernces. Your help will be very appreciated. Thank you

like image 697
Sanjay Joshi Avatar asked Aug 02 '13 14:08

Sanjay Joshi


People also ask

How can I save my shared preferences data?

Shared Preferences allow you to save and retrieve data in the form of key,value pair. In order to use shared preferences, you have to call a method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences.

Where is shared preference file stored android?

Android Shared Preferences Overview Android stores Shared Preferences settings as XML file in shared_prefs folder under DATA/data/{application package} directory. The DATA folder can be obtained by calling Environment. getDataDirectory() .

What can I store in shared preferences?

Shared Preferences is the way in which one can store and retrieve small amounts of primitive data as key/value pairs to a file on the device storage such as String, int, float, Boolean that make up your preferences in an XML file inside the app on the device storage.

What can I use instead of shared preferences in Android?

Jetpack DataStore is a new and improved data storage solution aimed at replacing SharedPreferences. Built on Kotlin coroutines and Flow, DataStore provides two different implementations: Data is stored asynchronously, consistently, and transactionally, overcoming most of the drawbacks of SharedPreferences.


1 Answers

You will have to convert each of your calendar to long using method Calendar.getTimeInMillis() and then save it in your preference using method putLong(key, long). You will have to change the key for each entry. Here is an example:

    SharedPreferences shrdPref = getSharedPreferences("sharePref",MODE_PRIVATE);
    SharedPreferences.Editor prefEditor = shardPref.edit();
       for(int i = 0; i<list.size();i++){
          long millis = list.get(i).getTimeInMillis();
          prefEditor.putLong("calendar"+i,millis);
          prefEditor.commit();
       }

You would then retrieve them with:

int i =0;
while(shrdPref.getLong("calendar"+i,0)!=0){
   Calendar cal = new GregorianCalendar();
   cal.setTimeInMillis(shrdPref.getLong("calendar"+i,0));
   list.add(cal);
   i++;
}

Bit of a hack, not sure if it works, try it and let me know.

like image 183
Quentin Golsteyn Avatar answered Oct 19 '22 23:10

Quentin Golsteyn