I would like to restore the state of my array after a change of orientation. But I don't see an aproprieted function for this case. What shoul I do?
public class PuzzlePiece extends android.support.v7.widget.AppCompatImageView {
public int xCoord;
public int yCoord;
public int pieceWidth;
public int pieceHeight;
public boolean canMove = true;
public PuzzlePiece(Context context) {
super(context);
}
This is my onSave method
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelableArray("image", pieces);
}
}
And data initalization:
ArrayList<PuzzlePiece> pieces;
Basically speaking, you need to implement two methods: saveData() and loadData(). These will use GSON library in order to transform any object into a string and then re-create them back. Then you save and restore your objects where ever you need. You may do so when onPause() method is called, or anywhere else.
Here is the code:
Firstly, add the following dependency line in your build.gradle:
implementation 'com.google.code.gson:gson:2.8.6'
Then you write those two methods
private void saveData(){
SharedPreferences sharedPreferences = getSharedPreferences("shared preferences id", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
Gson gson = new Gson();
String json = gson.toJson(arraylistOfYourObjects);
editor.putString("data_id", json);
editor.apply();
}
private void loadData(){
SharedPreferences sharedPreferences = getSharedPreferences("shared preferences id", MODE_PRIVATE);
Gson gson = new Gson();
String json = sharedPreferences.getString("data_id", null);
Type type = new TypeToken<ArrayList<YourObjectClass>>() {}.getType();
arraylistOfYourObjects = gson.fromJson(json, type);
if(arraylistOfYourObjects == null){
arraylistOfYourObjects = new ArrayList<>();
}
}
Here is a link to a complete video on how to do the whole thing with allthe details:
https://www.youtube.com/watch?v=jcliHGR3CHo
Related
I want to clear the data I am getting from SharedPreference, I try the following answer but didn't make my task:
1) how to delete sharedpreferences ,Quit and launch application from first actvity in android
2) clear the value of sharedpreferences
3) Remove Shared preferences key/value pairs
4) SharedPreferences Clear/Save
they are all removing value, after they write data into SharedPreference, like editor.remove and .clear...
I have write data into SharedPreference in Notification Activity Like this:
public static final String PREFS_NAME = "com.example.sociapp";
NotificationAdapter notificationAdapter1 = new NotificationAdapter(NotificationsActivity.this, NotificationList, NKeyList);
RnotificationList.setAdapter(notificationAdapter1);
isthereItem = notificationAdapter1.getItemCount();
Toast.makeText(NotificationsActivity.this, ""+isthereItem, Toast.LENGTH_SHORT).show();
//writing data into SharedPreference
SharedPreferences.Editor editor = settings.edit();
editor.putInt("changingicon",isthereItem);
//editor.commit();
editor.clear();
editor.apply();
And I am getting this int value in MainActivity Like this:
SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
// Reading from SharedPreferen
try {
//all I want to refresh below line everytime I start MainActivity.
int ChangeIcon = settings.getInt("changingicon", 0);
if (ChangeIcon == 0)
{
int valuebecomes = 0;
notificationIconSetting(valuebecomes);
}
else
{
int valuebecomes = 1;
notificationIconSetting(valuebecomes);
}
Toast.makeText(MainActivity.this, ""+ChangeIcon, Toast.LENGTH_SHORT).show();
}
catch (ClassCastException e){e.printStackTrace();}
The method I call, when I get int value from SharedPreference:
private void notificationIconSetting(int IconTochange)
{
if (IconTochange == 0) {
navigationView.getMenu().getItem(2).setIcon(R.drawable.notification);
}
else
{
navigationView.getMenu().getItem(2).setIcon(R.drawable.notificationalert);
navigationView.setItemIconTintList(null);
}
}
Actually I am getting an int value greater than 0 when there is a notification in the adapter, and when there is no notification in the adapter the int value is equal to 0, then I am using this value to change the notification icon.
When there is notification:
When there is no notification:
Now the problem is whenever I get a value, it remains the same until I clear app cache or Uninstall and then install again.
All I want to refresh the SharedPreference value every time I start MainActivity.
You want to remove one key/value from shared preference
here's how i do it.
public void clearSpecificKey(String key){
sharedPreferences.edit().remove(key).apply();
}
Few things to note :
You should create a generic class of Shared Preference Like below
public class SharedPrefs {
private static final String MY_PREFS_NAME = "YOUR-PREFERENCE-NAME";
private static SharedPreferences sharedPreferences;
private String masterKeyAlias;
public SharedPrefs(Context context) {
{
try {
masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC);
} catch (GeneralSecurityException | IOException e) {
e.printStackTrace();
}
}
try {
sharedPreferences = EncryptedSharedPreferences.create(MY_PREFS_NAME,masterKeyAlias,context,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM);
} catch (GeneralSecurityException | IOException e) {
e.printStackTrace();
}
}
public String getStrings(Context mContext, String key){
return sharedPreferences.getString(key, null);
}
public void putString(Context mContext, String key, String value ){
sharedPreferences.edit().putString(key, value).apply();
}
public boolean getBoolean(Context mContext, String key){
return sharedPreferences.getBoolean(key, false);
}
public void putBoolean(Context mContext, String key, boolean value ){
sharedPreferences.edit().putBoolean(key, value).apply();
}
public static void clear(Context mContext){
sharedPreferences.edit().remove("user").apply();
}
public void clearSpecificKey(String key){
sharedPreferences.edit().remove(key).apply();
}
}
Here how to use it
Declaration :
SharedPrefs sharedPrefs;
Initialization :
sharedPrefs = new SharedPrefs(context);
just call the methods you want to use to store value in shared preference like
sharedPrefs.putString(context,key,value)
masterKeyAlias is to secure my Shared preferences.
Add this your app gradle
implementation "androidx.security:security-crypto:1.0.0-beta01"
you can read more about it here Best Practices
I have and app in which contain listview and adapter. In listview I am storing data in ArrayList and want to store this ArrayList object in sharedpreference and retrieve the same in next activity and populate data in listview. How do I do that?
Code:-
listViewHolder.switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
new AlertDialog.Builder(mContext, R.style.AppCompatAlertDialogStyle).setTitle("Warning").setMessage("You want to whiteList this application?").setPositiveButton("YES", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//here i am adding items to another arraylist
List<WhiteListModel> res = new ArrayList<WhiteListModel>();
WhiteListModel whiteListModel = new WhiteListModel();
whiteListModel.setName(listStorage.get(position).getName());
whiteListModel.setPackName(listStorage.get(position).getPackName());
whiteListModel.setIcon(listStorage.get(position).getIcon());
res.add(whiteListModel);
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
SharedPreferences.Editor editor = sharedPrefs.edit();
Gson gson = new Gson();
String json = gson.toJson(res);
editor.putString("WhiteList", json);
editor.apply();
listStorage.remove(position);
notifyDataSetChanged();
listViewHolder.switchCompat.setChecked(false);
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
listViewHolder.switchCompat.setChecked(false);
}
}).show();
}
}
});
you can use Gson to convert your ArrayList into JSON which is in string format and then after you can put that string into shared preference, in next screen you have to make an array list from that JSON string.
Best way to implements what you are asking for is by using a library called
Gson, include it in your code like:
import com.google.gson.Gson;
using this, you will be able to convert your array list or any set of data to Json format, store it to Shared Preferences, then Retrieve them.
a simple example to do that is simple and easy to do, HERE
If you want to pass items from one Activity to the other you should use Parcelable.
Your class WhiteListModel should implement Parcelable like the documentation suggests you. Then you can do something like this to write the arraylist:
Intent intent = new Intent(<context>, <targetActivity>);
intent.putParcelableArrayListExtra("key", whiteListModel);
To get the arraylist you can:
whiteListModel = getIntent().getParcelableArrayListExtra("key");
You can also wrap your model class into another class and make this one parcelable.
public static void saveUserLoginDetail(Context context, LoginResponse response) {
Gson gson = new Gson();
String jsonString = gson.toJson(response);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
if (preferences != null) {
SharedPreferences.Editor editor = preferences.edit();
editor.putString(key, jsonString);
editor.commit();
}
}
public static LoginResponse getUserLoginDetail(Context mContext) {
LoginResponse loginResponse = new LoginResponse();
String jsonString = getStringPreference(mContext, PREF_LOGIN_RESPONSE);
loginResponse = new Gson().fromJson(jsonString, LoginResponse.class);
return loginResponse;
}
If you want to get back Array List of type any object like LoginResponse then use :
Type type = new TypeToken<List<LoginResponse>>(){}.getType();
List<LoginResponse> loginResponse= gson.fromJson(json, type);
Add Value to List
Set<String> set = new HashSet<String>();
set.add("11122");
set.add("222");
Save to shareprefarance set value using saveSharePrefaraceArrayList method
saveSharePrefaraceArrayList(set);
SharedPreference save method to save list of value
void saveSharePrefaraceArrayList( Set<String> setList){
SharedPreferences prefs=this.getSharedPreferences("yourPrefsKey",Context.MODE_PRIVATE);
Editor edit=prefs.edit();
Set<String> set = new HashSet<String>();
set.addAll(setList);
edit.putStringSet("myshareprekey", set);
edit.commit();
}
Finally get list value from shareprefarance
SharedPreferences prefs=this.getSharedPreferences("yourPrefsKey",Context.MODE_PRIVATE);
SharedPreferences.Editor edit=prefs.edit();
Set<String> set = prefs.getStringSet("myshareprekey", null);
ArrayList<String> arraylistValues = new ArrayList<String>(set);
If the data does not require an orderly arrangement and also does not contain duplicate elements.,you can try SharedPreferences.Editor#putStringSet()
add this line in dependencies in build.gradle
compile 'com.google.code.gson:gson:2.7'
UserObj.class
public UserObj {
String firstName,lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
save object
preferences.putString("user", new GsonBuilder().create().toJson(user));
load object
String str = preferences.getString("user", "");
if (!str.isEmpty()) {
user = new GsonBuilder()
.serializeNulls()
.create()
.fromJson(userString, UserObj.class);
}
I'm quite new to android, and I am having a problem with save and load data.
I am trying to make an app with a save and a load button, these buttons should save 2 ArrayLists with x- and y-coordinates.
I've tried doing it with SharedPreferences and it works until the apps restarts or the screen rotates.
When i take look in the app files the ArrayLists files are in the SharedPreferences folder, but my app will not load those if I press the load button.
Could anyone help we why this does not work when the app is restarted?
this is my load and save code:
public void saveArrayList(ArrayList aList, String s) {
SharedPreferences sharedPrefs = getSharedPreferences(prefs, MODE_PRIVATE);
editor = sharedPrefs.edit();
Gson gson = new Gson();
String json = gson.toJson(aList);
editor.putString(s, json);
editor.commit();
}
public void loadFloatList(ArrayList aList, String s) {
SharedPreferences sharedPrefs = getSharedPreferences(prefs, MODE_PRIVATE);
Gson gson = new Gson();
String json = sharedPrefs.getString(s, null);
Type type = new TypeToken<ArrayList<Float>>() {
}.getType();
aList = gson.fromJson(json, type);
}
Possible error; you are re-initializing gson and sharedPrefs which then takes a new state with the same key value. you should do this in onCreate.
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedPrefs = getSharedPreferences(PREFS, MODE_PRIVATE);
json = sharedPrefs.getString(JSON_KEY, "");
gson = new Gson();
}
it is best to use apply() because this save the value(s) of your sharedprefs as instance but commit() do this when the app closes.
public void saveArrayList(ArrayList aList) {
String json = gson.toJson(aList);
editor = sharedPrefs.edit();
editor.putString(JSON_KEY, json);
editor.apply();
}
public void loadFloatList(ArrayList aList, String s) {
json = sharedPrefs.getString(JSON_KEY, "");
Type type = new TypeToken<ArrayList<Float>>() {
}.getType();
aList = gson.fromJson(json, type);
}
note also the PREFS and JSON_KEY are final key values
private final String PREFS = "prefs";
private final String JSON_KEY = "json";
private SharedPreferences sharedPrefs;
private SharedPreferences.Editor editor;
private String json;
private Gson gson;
I can't obtain data from SharedPreferences.. After I click on a button, it executes AsyncTask in a different class named Background.
public class Background extends AsyncTask<Integer,Void,String>{
private Context context;
private AsyncResponse listener;
public Background(Context context,AsyncResponse listener) {
this.context = context;
this.listener=listener;
}
AsyncResponse is an interface that I've created to inform my MainMactivity that background work has been finished. It's just:
package com.example.pablo.zad3;
public interface AsyncResponse {
void TaskCompleted();
}
Then I want to pass the result to SharedPreferences:
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
SharedPreferences prefs = this.context.getSharedPreferences("MyPrefs",Context.MODE_PRIVATE);
prefs.edit().putString("A", s);
prefs.edit().commit();
listener.TaskCompleted();
}
But in my MainActivity I can't get the resulting string, it's like there was no A key in SharedPreferences (I always get "NO DATA"):
#Override
public void TaskCompleted() {
SharedPreferences prefs = this.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
if(prefs != null) {
String text = prefs.getString("A", "NO DATA");
editText2.setText(text);
}
}
I don't know what I'm doing wrong, can u help me?
The problem are those two lines:
prefs.edit().putString("A", s);
prefs.edit().commit();
at the first one you are opening SharedPreferences for edit and do the changes ...
now at the second line you are opening it again ... which causing the previous changes rollback ...
As the "family" of putXXX(...) method of the SharedPreferences.Editor class returns Editor itself you should do something like:
prefs.edit().putString("A", s).putXXX(...).putXXX(...).commit();
(by putXXX(...) I mean puting other preferences fx putInt("Z", 666))
now the changes will be saved
Use this for writing to preferences:
//Write to preferences
String s = "this is a test.";
SharedPreferences prefs = this.getSharedPreferences("MyPrefs",this.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("A", s);
editor.apply();
And this for retrieving information
//Fetch from preferences
SharedPreferences prefs2 = this.getSharedPreferences("MyPrefs", this.MODE_PRIVATE);
if(prefs2 != null) {
String text2 = prefs.getString("A","");
Log.d(LOG_TAG, "This is the string: "+text2);
}
The only thing that you missed is an editor.
I am trying to update a password generator I made to include the ability to save passwords, so I'm using SharedPreferences. I want to put the saved passwords inside a Set, but when I try to save the set using SharedPreferences.Editor's putStringSet method, Eclipse does not recognize it. When I hover over putStringSet in my code, the 2 quick fixes available are "Change to putString(...)" and "add cast to editor", but I don't think either of those helps.
This is what I have:
public void save(View v)
{
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
final SharedPreferences.Editor editor = prefs.edit();
savedPasswords = (Set<String>) getSharedPreferences("savedPasswordsList", 0);
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setItems(passwords, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface face, int num)
{
savedPasswords.add(passwords[num]);
editor.putStringSet("savedPasswordsList", savedPasswords);
editor.commit();
refreshSavedPasswordsList();
}
});
dialog.show();
}
putStringSet(...) was added at API 11 (Android v3.0.x onwards). My guess is you're targeting a version below that.
I implemented data storage using putStringSet and then needed to backport it to Gingerbread so I created a small class called JSONSharedPreferences.
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.SharedPreferences;
public class JSONSharedPreferences {
private static final String PREFIX = "json";
public static void saveJSONObject(SharedPreferences prefs, String key, JSONObject object) {
SharedPreferences.Editor editor = prefs.edit();
editor.putString(JSONSharedPreferences.PREFIX+key, object.toString());
editor.commit();
}
public static void saveJSONArray(SharedPreferences prefs, String key, JSONArray array) {
SharedPreferences.Editor editor = prefs.edit();
editor.putString(JSONSharedPreferences.PREFIX+key, array.toString());
editor.commit();
}
public static JSONObject loadJSONObject(SharedPreferences prefs, String key) throws JSONException {
return new JSONObject(prefs.getString(JSONSharedPreferences.PREFIX+key, "{}"));
}
public static JSONArray loadJSONArray(SharedPreferences prefs, String key) throws JSONException {
return new JSONArray(prefs.getString(JSONSharedPreferences.PREFIX+key, "[]"));
}
public static void remove(SharedPreferences prefs, String key) {
if (prefs.contains(JSONSharedPreferences.PREFIX+key)) {
SharedPreferences.Editor editor = prefs.edit();
editor.remove(JSONSharedPreferences.PREFIX+key);
editor.commit();
}
}
}
Usage:
//Below, the code to use putStringSet is commented out.
//Alternative JSONSharedPreferences is used instead
//Set<String> trainers = new TreeSet<String>();
JSONArray jTrainers = new JSONArray();
List<FilteredWorkoutVideo> videos = getAllFilteredVideos(prefs);
for (FilteredWorkoutVideo video : videos) {
//trainers.add(video.getTrainerName());
jTrainers.put(video.getTrainerName());
}
//e = prefs.edit();
//e.putStringSet(Constants.KEY_ALL_TRAINERS, trainers);
//e.apply();
JSONSharedPreferences.saveJSONArray(prefs, Constants.KEY_ALL_TRAINERS, jTrainers);
To work around this issue I created a SharedPreferencesCompat class:
In stores the StringSet in a string CSV-style.
It is possible to change ',' used in CVS by another delimiter.
public class SharedPreferencesCompat {
private final static String KEY_DELIMITER = "com.example.delimiter";
public static void setStringSetDelimiter(final SharedPreferences sharedPreferences, final String delimiter) {
final Editor editor = sharedPreferences.edit();
editor.putString(KEY_DELIMITER, delimiter);
editor.commit();
}
public static Set<String> getStringSet(final SharedPreferences sharedPreferences, final String key) {
final Set<String> out = new LinkedHashSet<String>();
final String base = sharedPreferences.getString(key, null);
if (base != null) {
out.addAll(Arrays.asList(base.split(sharedPreferences.getString(KEY_DELIMITER, ","))));
}
return out;
}
public static void putStringSet(final SharedPreferences sharedPreferences, final String key,
final Set<String> stringSet) {
final String concat = StringUtils.join(stringSet, sharedPreferences.getString(KEY_DELIMITER, ","));
final Editor editor = sharedPreferences.edit();
editor.putString(key, concat);
editor.commit();
}
}
Note:
It depends on apache StringUtils.join() method.
You can easily swap this out for another one.
I know this thread is a old, but I just ran into the same issue and my solution was to use a global HashTable to keep track of my strings, which were actually file names. I also needed a status associated with with each file, so I implemented the following simple class:
public class Globals
{
public static enum SendStatus
{
NOT_SENT,
SEND_SUCCESS,
SEND_ERROR
}
public static volatile Hashtable<String, SendStatus> StoredFilesStatus;
}
}