android SharedPreferences synchronous problem adding array - android

hi
I have a scenario like this
I made a chat program where a user can ad friends just like in yahoo messenger or hotmail messenger. If there are many friend requests coming in to one user i´m saving them dynamically
like this: (Every request(string) look like this "queryaddnewfriend:name:UUID")
String msg = intent.getStringExtra("payload");
String[] split = msg.split(":");
String name = split[1];
String UUID = split[2];
if(msg.startsWith("queryaddnewfriend")){
//queryaddnewfriend:name:UUID
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext() );
String frn = prefs.getString("friendrequest1", "");
if(frn == ""){
SharedPreferences.Editor editor = prefs.edit();
String newReq = name.concat(":");
newReq = newReq.concat(UUID); //create the name:UUID string
editor.putString("friendrequest".concat( Integer.toString(1)), newReq);
editor.commit();
}else{
for(int index = 1; index < 1000; ++index) {
String line = prefs.getString("friendrequest".concat( Integer.toString(index)), "");
if(line == ""){
Log.d(TAG,"create new *********************************************");
String newLine = name.concat(":");
newLine = newLine.concat(UUID); //create the name:UUID string
SharedPreferences.Editor editor = prefs.edit();
editor.putString("friendrequest".concat( Integer.toString(index)), newLine);
editor.commit();
break;
}
}
}
So my SharedPreferences has non, one, or many rows like this
Dynamically added (notes the "friendrequest1" incrementation )
prefs.getString("friendrequest1","queryaddnewfriend:name:UUID");
prefs.getString("friendrequest2","queryaddnewfriend:name:UUID");
prefs.getString("friendrequest3","queryaddnewfriend:name:UUID");
The friend requests are showed to the user one by one starting with friendrequest3.
The problem comes when the user accept a friend request.
I have to remove the friendrequest3 and at the same time there could be a
new friend request coming in and the code above is executed adding a new friendrequest4.
Im using C2DM so I have no control when Google cloud is executing the above code.
When i remove "friendrequest3" because user has responded ACCEPT or REJECT friend
I will do editor.remove("friendrequest3") editor.commit();
But if the above code has added "friendrequest4" my code will fail.
the complexity of this code is now quite high and i guess one can make it higher
and at the same time increasing the "bug factor"
Any thought about doing this better would be nice, thanks!

If it were me, I think I'd be using Sqlite for this, not preferences. It's much easier to manage and process rows of data, and you'll also find it's quicker. I've found that writing a series of preferences in quick succession is actually very, very slow, as writing to flash RAM can sometimes take a lot longer than you'd expect. For that reason when I write to prefs, I usually fire off a new thread to do it.
But my suggestion is.... use a database Sqlite to store the incoming messages, and put them behind a content provider.

Related

Android: set timer to allow opening activity

I have a spin to win activity in my app that user can use to get coins.
i would like to make that after the user use the spinner he would have to wait for 8 hours before being allowed to open the spin activity again.
how to do that ?
one way will be:
determine those 8 hours from the moment he spinned ->
long duration = System.currentTimeMillis() + your8hours;
save this data (sharedpreference) ->
SharedPreferences sharedPreferences = new SharedPreferences();
sharedPreferences = getSharedPreferences("MYDURATIONSAVED", Context.MODE_PRIVATE)
Editor editor = sharedPreferences.edit();
editor.putInt("durationSaved", duration);
editor.apply();
in the new session, you retrieve the data ->
long alreadyExistingDuration = sharedPreferences.getLong("durationSaved", -1);
and simply compare it in order to check if the user is allowed or not to spin again ->
long check = System.currentTimeMillis();
if(check >= alreadyExistingDuration){
allow...
}
if you would have put a sample of your code I would have use it
for example, if you use sqlite, firebase or files to store your data, it may have been a different answer
But the data that you need to store is primitive so sharedpreference should be enough

Looping out JSON Array using ArrayList

I am trying to learn retrofit and I have made successful attempts at posting data and now I am trying to retrieve JSON array which looks as follows:
{
"result": "success",
"message": "All Questions Have Been Selected",
"question": {
"all_question_ids": ["1","2","3"]
}
}
I am using the following getter
public ArrayList getAll_question_ids(){
return all_question_ids;
}
I am retrieving using Retrofit as follows
if (resp.getResult().equals(Constants.SUCCESS)) {
SharedPreferences.Editor editor = pref.edit();
Log.d("Question_IDs", "getAllQuestionID() = " + response.body().getQuestion().getAll_question_ids() );
editor.putString(Constants.All_QUESTION_IDS,((resp.getQuestion().getAll_question_ids().toString())));
editor.apply();
}
progress.setVisibility(View.INVISIBLE);
It is here that I am stuck, as I am retrieving the array ok but I am unsure how to loop out the Array which is now stored in Shared Preferences.
When I place a toast to show me how the IDs are coming across, my toast confirms the data as [1,2,3]
The goal is to add a dynamic button and the individual ID, i.e button 1, button 2 etc every-time the loop is iterated.
I have tried the following:
String questionNumber = pref.getString(Constants.All_QUESTION_IDS, "");
for (int i =0; i < questionNumber.length(); i++) {
try {
/*Dynamically create new Button which includes the question name
*/
AppCompatButton btn_question = new AppCompatButton(getActivity());
/*LayoutParams (int width, int height,float weight)
As LayoutParams is defaulted in px, I have called a method called dpToPX to make sure
the dynamically added EditText is the same size on all devices.
*/
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(dpToPx(280), dpToPx(45), 1);
btn_question.setBackgroundColor(Color.parseColor("#3B5998"));
btn_question.setTextColor(Color.WHITE);
// btn_question.setText(String.valueOf(x));
btn_question.setText("Question "+ pref.getString(Constants.All_QUESTION_IDS,""));
btn_question.setGravity(Gravity.CENTER);
//generate unique ID for each new EditText dynamically created
View.generateViewId();
//Log.d("TEST VALUE", "Question1 generated ID = " + btn_question.generateViewId());
params.setMargins(0, dpToPx(10), 0, dpToPx(10));
btn_question.setPadding(0, 0, 0, 0);
btn_question.setLayoutParams(params);
allEds.add(btn_question);
mLayout.addView(btn_question);
} catch (Exception e) {
Log.d(TAG, "Failed to create new edit text");
}
}
However the above is adding the value as it appears in the array e.g [1,2,3] which is obviously not what I want.
I have added a photo in case my explanation isn't clear. I want a button with 1 number added to it each time the loop iterates but I am unable to figure this out.
I have looked through lots of resource but cannot find an answer that is relevant to my problem, although, if there is, I am not familiar enough to recognise a similar issue.
If someone can offer some assistance, I would appreciate it!
When you call editor.putString(Constants.All_QUESTION_IDS,((SOMETHING.toString())));, what is actually stored depends on the implementation of the toString method in the type of SOMETHING (in this case String[]). So avoid doing that. Instead, since you're already using Gson or Jackson (or others), store the question_idsas JSON:
final String jsonIds = gson.toJson (resp.getQuestion().getAll_question_ids());
editor.putString(Constants.All_QUESTION_IDS, jsonIds);
Your actual stored value no longer depends on the implementation of something that you don't control (String[].toString). It is a valid JSON array and regardless of what tool/library you use to read it back, it's valid.
Now, to read back the stored data:
final String storedJson = pref.getString(Constants.All_QUESTION_IDS, null);
if (null == storedJson) {
// TODO: No question ids found
}
final String[] ids = gson.fromJson (storedJson, String[].class);
for (int i = 0; i < ids.length; i++) {
// make your buttons
}
This is a problem of saving and then reading out a List of items (in this case, String instances).
You've chosen to save the list by calling editor.putString() with a value of getAll_question_ids().toString(). That toString() call is going to return a string representation of your list, or, in other words, a String instance with the value [1, 2, 3]. At this point, you no longer have a List proper, but a String that looks like a list.
This is all technically fine, but it means you have to take this into account when you're trying to read out that list.
You've written this to read the list back out:
String questionNumber = pref.getString(Constants.All_QUESTION_IDS, "");
Once this line executes, questionNumber will be a String instance with the value [1, 2, 3]. Again, this is fine, but now we come to the key point: we have to convert this String back into a List.
If you know for sure that the values in this list won't have commas in them, you can do it easily:
Trim the braces off the string using substring()
Create a String[] using split()
Convert your array to a list using Arrays.asList() (you could even skip this step since iterating over an array is just as easy as iterating over a list)
Put that together and you get:
String questionNumber = pref.getString(Constants.All_QUESTION_IDS, "");
questionNumber = questionNumber.substring(1, questionNumber.length() - 1);
String[] array = questionNumber.split(", ");
List list = Arrays.asList(array);
At this point, you can iterate over your array or list:
for (String value : list) {
...
btn_question.setText("Question " + value);
...
}

How to save variables with SharedPreferences

I have my own Objects which I need to store for later use. The User saves this object, it is turned into a JSON String, then when the User is connected to a network, the JSON String is turned back into the object operations are performed on it.
My problem is that, at run time, how do I know how to store the object?
i.e
Gson gson= new Gson();
String pointOfInterest = gson.toJson(point);
SharedPreferences.Editor sharedprefEditor = application_shared_preferences.edit();
sharedprefEditor.putString(?KEY?,pointOfInterest);
What can I use for the value of KEY? If I use an index, it will get reset every time I open or close the app, and this will replace my Objects.
Edit
Sorry I didn't make this clear enough, the method that the above code is in can be run an arbitrary number of times and there could be several pointsOfInterest to store.
First of all, if you use an index, the Preference will stay forever:
For instance:
sharedprefEditor.putString("JSON569",pointOfInterest);
You can also save the index in an other preference; for instance separated by a column:
sharedprefEditor.putString("indexes","569;789;852");
You can, easily check if an instance exists:
myPreference.getString("JSON789","").contentEquals("");
Or get all your instances:
for (int anIndex:indexes)
    Log.i("TAG","Current value: "+myPreference.getString("JSON"+anIndex,""));
Please xplain a little bit more your question, I see no difficulties there/
You can name the key whatever you want, just make it consistent. One way to do it is make a constant in your class for it:
public class MyClass {
private static final String OBJECT_KEY = "myObjectKey";
...
Then when you save your object:
Gson gson= new Gson();
String pointOfInterest = gson.toJson(point);
SharedPreferences.Editor sharedprefEditor = application_shared_preferences.edit();
sharedprefEditor.putString(OBJECT_KEY,pointOfInterest);
When you load it, just use OBJECT_KEY to get a string out of the shared preferences:
String objectString = sharedPrefs.getString( OBJECT_KEY, "" );

Using Shared Preferences with arrays

EDIT:
OK It turns out this code was working (more or less) I'd left in a line that reset the booleans I was trying to change. Thanks everyone for the help though.
Having trouble using SharedPreferences to read in saved array data when my app starts.
My _dPad Boolean and my _FreePlay Integer loads, saves and passes to and from my _renderer without any problems.
The trouble starts when I try and use some arrays
easteregg[] only has 2 entries right now so obviously I could just just turn them into separate variables but I wish to add more arras of longer length so this makes a convenient test example.
I've noted on the code what appears to happen (the easteregg[] settings just doesn't appear to have changed)
to read data:
// Read saved preferences
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
_renderer._dPad = prefs.getBoolean("_dPad", false); // * works ok *
_renderer._FreePlay = prefs.getInt("_FreePlay", 1); // * works ok *
_renderer.easteregg[0] = prefs.getBoolean("easteregg[0]", false ); // * not working
_renderer.easteregg[1] = true; // * even this is not working
setRenderer(_renderer);
to write data:
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
// As good a time as any to save current config
save = false ; // don't commit if nothing changed.
SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(getContext());
SharedPreferences.Editor editor = prefs.edit();
if (_renderer._dPad != prefs.getBoolean("_dPad",false)){ save = true ;
editor.putBoolean("_dPad", _renderer._dPad);}
if (_renderer._FreePlay != prefs.getInt("_FreePlay",1)){ save = true ;
editor.putInt("_FreePlay", _renderer._FreePlay);}
editor.putBoolean("easteregg[0]", _renderer.easteregg[0]);
editor.putBoolean("easteregg[1]", _renderer.easteregg[1]);
if (save == true){editor.commit();}
}
And in the .renderer class
// START SAVE DATA
public boolean _dPad ; // false no Virtual Pad *Works Fine*
public int _FreePlay ; // 1 = no free play *Works Fine*
public boolean[] easteregg = new boolean[2]; *Values don't load or save*
//public boolean easteregg[]; // tried this first *CAUSES CRASH*
// END SAVE DATA
Do I have to convert the arrays to strings? I don't get how to change them.
I put your code into a quick activity, creating just the shell of the renderer class as you have above and found that your save boolean is false, so it never commits the preferences.
I forced the save to true, and played around with it and everything worked fine from there.
I'd recommend adding checks to the easter eggs the same as you have for any other preference; test to see if the current value is the same as the saved value, and if not, set the save flag.
I would suggest saving the array as a string in a single variable. It appears you have an array of booleans. So loop through it to make it a series of either ints (0, 1) or the string "true" or "false" then save it to an int or string.
I suspect the probelm might be that your setting name contains square brackets. I think that in key value names, the key name must be a valid variable name. And square brackets are not allowed in variable names.
However i would also expect this to throw an error. Does the code work if you name you settings "easteregg_01" and "easteregg_02"?
The best solutions would be to convert your array into JSON string and store it as preference value. If you have small amount of data, you can as well stick with org.json classes provided by android. If you have more data, GSON pull parser would be better, as it utlizes pull parser. And if you are really lazy, you grab my small databinding library and do:
String jsonState = preferences.getString(GAME_STATE, null);
StateStorage storage = JSONUnmarshaller.unmarshall(new JsonReader(new
StringReader(jsonState)), StateStorage.class);
and it will instantiate java class for you and fill in the data. And to save:
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
StringWriter writer = new StringWriter();
JsonWriter jsonWriter = new JsonWriter(writer);
JSONMarshaller.marshall(jsonWriter, ss);
editor.putString(GAME_STATE, writer.toString());
editor.commit();
Databinding library is available on github, or from maven central:
https://github.com/ko5tik/jsonserializer
PS: at the moment I work on injection of preference values ( at the moment primitives only):
https://github.com/ko5tik/andject

Android sharedpreferences into arraylist

I created a simple game. At the end the user's name and score is supposed to get into a highscore list. For this i would like to store these data in sharedpreferences. I saw a post and i am trying to apply it to my app but it force closes. I don't even know if this is the right thing i am doing. So i put these keypairs (player, score) into an arraylist. From there i can get the values out into a listview.
This is just an example.
SharedPreferences.Editor scoreEditor = myScores.edit();
scoreEditor.putString("PLAYER", "Thomas");
scoreEditor.putString("SCORE", "5");
scoreEditor.commit();
final ArrayList<HashMap<String,String>> LIST = new ArrayList<HashMap<String,String>>();
Map<String, ?> items = myScores.getAll();
for(String s : items.keySet()){
HashMap<String,String> hmap = new HashMap<String,String>();
hmap.put("PLAYER", s);
hmap.put("SCORE", items.get(s).toString());
LIST.add(hmap);
}
Toast.makeText(Start.this, "LIST size: "+LIST.size(), Toast.LENGTH_LONG).show();
For me it would also be okay if i store these data like this:
scoreEditor.putString("DATA", "Thomas" + "-" + "5");
and put that into ArrayList<String> LIST = new ArrayList<String>();
but i don't know how to do it.
Could you guys help me with this?
Edit: So i could go another way as Haphazard suggested. I put together this code, but i don't know if this is the way to do it. I haven't tested it yet, as sg is wrong with the sharedpreferences and i am still trying to figure it out.
SharedPreferences.Editor scoreEditor = myScores.edit();
scoreEditor.putString("DATA", "Thomas" + "-" + "5");
scoreEditor.commit();
HashSet<String> hset=new HashSet<String>();
hset.addAll((Collection<? extends String>) myScores.getAll());
ArrayList<String> LIST = new ArrayList<String>(hset);
The SharedPreferences Editor does not accept Lists but it does accept Sets. You could convert your List into a HashSet or something similar and store it like that. When your read it back, convert it into an ArrayList, sort it if needed and you're good to go.
Please note that the Set has to be a set of Strings so you will have to stick with your "Thomas" + "-" + "5" setup.
Edit: To your new update, I was thinking more of something like
//Retrieve the values
Set<String> set = new HashSet<String>();
set = myScores.getStringSet("key", null);
//Set the values
Set<String> set = new HashSet<String>();
set.addAll(listOfExistingScores);
scoreEditor.putStringSet("key", set);
scoreEditor.commit();
That code is untested, but it should work
EDIT: If your API level is below the get/setStringSet() level then you can try this:
1) Turn your list of high scores into a delimited string. That means if you had ["Tom, 1", "Ed, 5"] you could loop through it and turn it into a String like "Tom, 1|Ed, 5". You can easily store that using setString(..).
2) When you want to read the values back, perform a getString(..) and then String.split("|") to get the original list back. Well, it returns an array but that can be converted to a list easily enough.

Categories

Resources