As simple as saving a Hashset in the SharedPrefs - android

I was trying to write a simple method for my android application. This method should find an integer [0-49] that isn't contained in a HashSet. Afterward should save the HashSet in the sharedPrefs using the putStringSet() method. Below I provide the code I have written.
HashSet aHashSet;
SharedPreferences sharedPreferences;
int id;
sharedPreferences = getSharedPreferences("prefs", MODE_PRIVATE);
aHashSet = (HashSet) sharedPreferences.getStringSet("aHashSet", null);
if (aHashSet == null)
aHashSet = new HashSet();
for (id = 0; id < 50; ++id) {
if (aHashSet.contains(id))
continue;
else {
aHashSet.add(id);
break;
}
}
Log.d("myClassName", String.valueOf(sharedPreferences.edit().putStringSet("aHashSet", aHashSet).commit()));
return id;
I have checked the logs every time I add something to the set and save it in the shredPrefs I get a true (as a successful) commit. While this worked fine while the app was running, After the application was closed the HashSet wasn't saved correctly, and the starting number was always 0.
So I tried to debug this, I thought that I'll use a String and I will append the numbers to it and then save it, in order to check if the problem is that I cannot save the Hashset correctly, so I add this logic as below.
HashSet<String> aHashSet;
int id;
aHashSet = (HashSet) sharedPreferences.getStringSet("aHashSet", null);
String checkingString = sharedPreferences.getString("cs","");
if (aHashSet == null)
aHashSet = new HashSet();
for (id=0;id<50;++id){
if(aHashSet.contains(String.valueOf(id)))
continue;
else{
aHashSet.add(String.valueOf(id));
break;
}
}
checkingString+= id;
Log.d("myClassName",String.valueOf(sharedPreferences.edit().putStringSet("aHashSet",aHashSet).commit()));
Toast.makeText(this, checkingString, Toast.LENGTH_SHORT).show();
sharedPreferences.edit().putString("cs",checkingString).commit();
return id;
Now what I was expecting to see was: the checkingString to be appended and saved correctly, but the HashSet to lose the added values every time the app is restarted. However, now everything worked as it should, the HashSet was saved and restored correctly every time the app was restarted. Can someone explain to me how is this possible? the only change I did was to add another string to the sharedPrefs, how could this influence the HashSet? Thank you for your time

Hope this will help you. For this type of problem I have made two function for storing and retrieving multiple value using Hashmap
Store data into preference
public static void storePreference(HashMap<String, String> parameters) {
SharedPreferences.Editor editor = ManageMyAppsApplication.sharedPref
.edit();
Iterator<Map.Entry<String, String>> it = parameters.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> pairs = it.next();
editor.putString((String) pairs.getKey(), (String) pairs.getValue());
}
editor.commit();
}
Retrieving data from preference
public static HashMap<String, String> getPreference(String[] keys) {
HashMap<String, String> parameters = new HashMap<String, String>();
for (String key : keys) {
parameters.put(key,
ManageMyAppsApplication.sharedPref.getString(key, null));
}
return parameters;
}

Related

How can I add elements inside HashMap and save it? [Android]

I'm new with HashMap, how can I save permanently and add other items when I reopen the application?
For example:
private HashMap<String, Recognition> registered = new HashMap<>();
public void register(String name, Recognition rec) {
registered.put(name, rec);
}
I can see all items inside registered using:
for (Map.Entry<String, Recognition> entry : registered.entrySet()) {
final String name = entry.getKey();
... }
But when I close and reopen app, I can't see all objects saved inside registered.
I see lot of people using SharedPreferences, but I don't know how to add items inside the pre-saved hashmap.
You can do it indirectly this way:
//writing into file
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString( key, hashmap.getValue() );
editor.commit();
//reading from file
SharedPreferences pref = getPreferences(Context.MODE_PRIVATE);
for( i = 0;i < size;i++) )
pref.getString( i , defaultValue );

SharedPreferences not saving all the data after restart

I am new to Android App Development and I am supposed to make a TodoList App for a course. But the SharedPreference in my code is not working. I dont know if I'm supposed to use it in a specific way in a specific method like onCreate or onStop.
It is saving the first input the user is entering permanently, but in the same position:
(The "task0" is what I used to track the different variable names I used as argument for "putString" in addStuff method, to avoid replacing values)
It is saving the inputs after that in the same session, but if the user ends that session, all those values after "t" are gone. If the user restarts the app and inputs something else (like "g"), it is saving "g" in that same 3rd position.
I have basic Java knowledge and I tried to understand what is going on using it, but failed. Please let me know where is the mistake and how to use SharedPreferences properly.
public class TodoActivity extends AppCompatActivity {
public ArrayList<String> items;
public ArrayAdapter<String> itemsAdapter;
public ListView list;
public String s;
public EditText taskBox;
public static final String filename = "itemsList";
public TextView text;
public static int counter = 0;//counter starting at 0 no matter what, everytime the app starts
public String newtask= "task";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_todo);
list = (ListView) findViewById(R.id.list1);text = (TextView) findViewById(R.id.text1);
taskBox = (EditText) findViewById(R.id.box);
s = taskBox.getText().toString();
items = new ArrayList<String>();
itemsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
list.setAdapter(itemsAdapter);
//add items to list
items.add("First Item");
items.add("Second Item");
//restore
SharedPreferences sp = this.getSharedPreferences("itemsList", 0);
//checking if it stores the previous values, this gives the last input but not the previous ones after restarting the app
String dummyname = "task";
text.setText(String.valueOf(counter));//since counter is again at
for(int c=0; c<=50; c++){
String num = String.valueOf(c);
dummyname = dummyname + num;
String x = sp.getString(dummyname, "not found");
if (x.equalsIgnoreCase("not found")){
counter=c-1;
break;
} else {
items.add(x);
text.setText(dummyname);
}
}
}
public void addItem(View v){
s = taskBox.getText().toString();
itemsAdapter.add(s);//adding the new task as string
String temp = String.valueOf(counter);
newtask = "task" + temp;
//trying to store the new tasks with different variable names to avoid being replaced
text.setText(newtask);
SharedPreferences sp = this.getSharedPreferences("itemsList", 0);
SharedPreferences.Editor e = sp.edit();
e.putString(newtask,s);
e.apply();
counter++;
}
}
If you have relatively small collection of key-values that you would like to save,
You should use Shared preference API
Read from the shared preference:
Pass the key and value you want to write,create a SharedPreferences.Editor by calling edit() on your SharedPreferences.
Pass key and values you want to save by using this method putInt() ,putString() ,Then call commit() to save the changes. For example:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("KeyName", newHighScore);
editor.commit();
Write from the shared preference:
To retrieve values from a shared preferences file, call methods such as getInt() and getString(),
providing the key for the value you want, and optionally a default value to return if the key isn't present. For example:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger(R.string.saved_high_score_default);
long highScore = sharedPref.getInt("KeyName", defaultValue);
Two things :
1) To initialize SharedPreferences use :
sharedPreferences = getSharedPreferences("itemsList", Context.MODE_PRIVATE);
2) Where are you calling addItem() method??
The problem is about the Tag you use to save items. See this Line :
dummyname = dummyname + num;
You add item by this format :
task0
task1
task2
but you are getting values in this format
task0
task01
task012
Just change these two line of code :
//dummyname = dummyname + num;
//String x = sp.getString(dummyname, "not found");
String newDummy= dummyname + num;
String x = sp.getString(newDummy, "not found");

Shared prefrence overwrites

I save a string set in shared preferences that loads to my watchlist. When the user clicks on a button, in the movie's page, it takes the movie's name, title and release date and store them in a JSON object. All of them are stored as String. The problem persists when I try to add a movie in the watchlist, each new movie I add, the last movie gets overwritten, which means I can only have one movie in my watchlist at all time.
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences sharedPreferences = getSharedPreferences("WatchlistData", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
JSONObject obj = new JSONObject();
try {
obj.put("name", name);
obj.put("date", releaseDate);
obj.put("platform", platform);
} catch (JSONException e) {
e.printStackTrace();
}
Set<String> set = new HashSet<String>();
set.add(obj.toString());
editor.putStringSet("setOfStrings", set);
editor.commit(); //saved
}
});
Watchlist fragment OnCreateView
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_watchlist, container, false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.watchlist);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mWatchlistAdapter = new WatchlistAdapter(getActivity(), loadFromStorage());
mWatchlistAdapter.notifyDataSetChanged();
mRecyclerView.setAdapter(mWatchlistAdapter);
return view;
}
The loadFromStorage, loads my xml file in the recyclerview, this method is passed in my adapter constructor.
SharedPreferences mPrefs = getActivity().getSharedPreferences("WatchlistData", Context.MODE_PRIVATE);
ArrayList<watchlist> items = new ArrayList<watchlist>();
Set<String> set = mPrefs.getStringSet("setOfStrings", null); //retrieving set of strings
if (set == null){
Toast.makeText(getActivity(), "No data found", Toast.LENGTH_LONG).show();
} else {
//for every string in set
for (String s : set) {
try {
JSONObject jsonObject = new JSONObject(s); //for every JSONObject String
String name = jsonObject.getString("name");
String date = jsonObject.getString("date");
String platform = jsonObject.getString("platform");
watchlist newGame = new watchlist();
newGame.setGame(name);
newGame.setDate(date);
newGame.setPlatform(platform);
items.add(newGame);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
return items;
}
By default SharedPreferences replaces the old data with the newer one. What you can do is, get the old data first then append with with the newer one,
//Extract the value stored in SharedPreferences
String value = prefs.getString(<Key>, <DefaultValue>);
//Append to the extracted value
String appendedValue = append(value, newValue);
//Write the result back to SharedPreferences
editor.putString(<Key>, appendedValue).commit();
SharedPreferenced are made for lite weight data but not for heavy storage. So, according to your requirement you should write the data to a JSON/XML file in the external storage and read it back later.
it looks like you are just overwriting the stringset in SharedPreferences every time you put something there, which is why you only ever see the last result:
Set<String> set = new HashSet<String>();
set.add(obj.toString());
editor.putStringSet("setOfStrings", set);
You are just created a new 'set' on each click and saving that to "setOfStrings".
I was considering a similar implementation myself for a project, but saw that it became an issue manipulating the StringSet from SharedPreferences. For reference, [documentation on Android developers site](http://developer.android.com/reference/android/content/SharedPreferences.html#getStringSet(java.lang.String, java.util.Set))
states,
"Note that you must not modify the set instance returned by this call. The consistency of the stored data is not guaranteed if you do, nor is your ability to modify the instance at all."
There are workarounds to this, but from what I understand it is better to avoid using SharedPreferences for data such as this which will be manipulated often, as it is just saved as an xml file prone to issues- you may want to consider implementing that information in a SQLite database.

How to keep some static data for future use in a spinner?

When the app starts, I got some static data (array) from the server, and I need to use this data for a spinner, on different activities. So this data should be available from different activities at different times to create a spinner.
How can I make this? How can I generate a spinner in code (spinner data is key = value)?
What type of data contains that array? Maybe you can use SharedPreferences to save it.
You can read about generating spinner here. CountryAdapter class is example of making spinner adapter which displays items in spinner drop down list.
EDIT:
Put your data into Hashtable and create 2 methods to save and read your data from SharedPreferences:
public boolean saveData( Hashtable<String, String> myData) {
Editor editor = getApplicationContext().getSharedPreferences("myData", Context.MODE_PRIVATE).edit();
for (String key : myData.values()) {
editor.putString(key, myData.get(key));
}
return editor.commit();
}
public Hashtable<String, String> restoreData() {
Hashtable<String, String> myData = new Hashtable<String, String>();
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("myData", Context.MODE_PRIVATE);
int size = sharedPreferences.getInt("size", 0);
for(int i =0; i < size; i++){
myData.put(sharedPreferences.getString("key"+i, "key+1"), sharedPreferences.getString("value"+i, "value+1"));
}
return myData;
}

Is it possible to get the "key" value from a SharedPreferences file if I know the value it is paired with? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Can I get the key from SharedPreferences if a know the associated value?
The code below I use to populate an AlertDialog from a SharedPreferences file and sort it alphabetically by value.
When the user clicks an option in the AlertDialog list it launches
setServer(prefsCharSequence[i]);
prefsCharSequence[i] returns the value for the option chosen.
I want to use the "key" for that value. How do I go about getting this given my current code? now I know the value I was hoping it would be a simple "prefsCharSequence[i].getKey()" or something similar.
public void openServerDialog() {
final SharedPreferences myPrefs = this.getSharedPreferences("FileName", MODE_PRIVATE);
TreeMap<String, ?> keys = new TreeMap<String, Object>(myPrefs.getAll());
for (Map.Entry<String, ?> entry : keys.entrySet()) {
Log.i("map values", entry.getKey());
//some code
}
List<Pair<Object, String>> sortedByValue = new LinkedList<Pair<Object,String>>();
for (Map.Entry<String, ?> entry : keys.entrySet()) {
Pair<Object, String> e = new Pair<Object, String>(entry.getValue(), entry.getKey());
sortedByValue.add(e);
}
// Pair doesn't have a comparator, so you're going to need to write one.
Collections.sort(sortedByValue, new Comparator<Pair<Object, String>>() {
public int compare(Pair<Object, String> lhs, Pair<Object, String> rhs) {
String sls = String.valueOf(lhs.first);
String srs = String.valueOf(rhs.first);
int res = sls.compareTo(srs);
// Sort on value first, key second
return res == 0 ? lhs.second.compareTo(rhs.second) : res;
}
});
for (Pair<Object, String> pair : sortedByValue) {
Log.i("map values", pair.first + "/" + pair.second);
}
Collection<?> stringArrayList = keys.values();
final CharSequence[] prefsCharSequence = stringArrayList.toArray(new CharSequence[stringArrayList.size()]);
new AlertDialog.Builder(this)
.setTitle(R.string.server_title)
.setItems(prefsCharSequence,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface, int i) {
setServer(prefsCharSequence[i]);
}
})
.show();
}
I'm trying to iterate over and grab the key for the value I have, so far I have this but don't know where to go next:
CharSequence server = i;
SharedPreferences myPrefs = this.getSharedPreferences("FileName", MODE_PRIVATE);
Map<String, ?> items = myPrefs.getAll();
for(String s : items.keySet()){
}
i equals the value for the key I want, is this heading in the right direction?
As Tim suggested, you can refer to getAll() function of SharedPreferences class.
This will return you Map<String,?> object, you can use keySet() function of Map class for getting the keySet and for iterating use a foreach loop of Java.
Hope this helps.
If I understand your question correctly, prefsCharSequence is an array of the values from your SharedPreferences object. If that is the case, why not just keep an array of keys from the prefs? The value would be prefsCharSequence[i], and the key would be something like prefsCharSequenceKeys[i].

Categories

Resources