I have stored data with SharedPreference, I would like to retrieve them now by going through functions
The stored data:
dataUser = getApplicationContext().getSharedPreferences("userData", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = dataUser.edit();
editor.putString("date", dateString);
editor.commit();
My ProfileModel:
public class ProfileModel {
private String date;
public ProfileModel() {
}
public ProfileModel(String date) {
this.date = date;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
From the ProfileModel, I want to retrieve the value of the putString ("date", ...)
with getDate()
So what gives in a text this dateTxt.setText(String.valueOf(model.getDate()));
How I relate the ProfileModel to my stored data ?
(Having already done this with Firebase, connect the two data this:
ProfileModel model = snapshot.getValue(ProfileModel.class);
The snapshot
this is the name of the DataSnapshot, so I have to find something similar in SharedPreference
2
Answers
implement this library on build.gradle
you can save the entire custom object in shared pref in string by using gson library.
To Save any model ->
For Getting the object
Now you can access your obj like all other model class
One way to do it would be putting the SharedPreferences logic in the model constructor:
This would be an additional constructor along with the 2 you already have.
Then use it like this:
This should work, cheers!