Question

I wanted some feedback on this approach. My apologies if this is confusing or nonsensical. (thats probably why I'm asking because im unsure.)

My app auto assigns clientID's to a textview at first launch - is auto set to 100. To do this from that point forward I store an int value to sharedpreferences. At Oncreate I run a check against this specific ID in the sharedpreferences and later it either increments or assigns back to 100. The challenge was that sharedpreferences (that I know of) can only take a string value i.e. (editor.putString("CLIENTID", ID);) or editor.putInt("ID", c);

I wanted to avoid using a database for this portion to minimize overhead (could be wrong) There are validations in place that prevent this from being incremented unless certain conditions are met.

c is my persisted counter used in the shared preferences.

Here's what I did at oncreate:

public String ID; //global
int checkCount = prefs.getInt("ID", c); //get the current ID value currently set or last set.

if( checkCount > 100)
{
c = checkCount; //whatever the value of c is from its latest increment the counter c set to this
}
else if (checkCount <= 100)
{
c  = 100; //base value of clientIDs //assign base value for starting clientIDs
}

ID = "" + c + ""; //sets value of the counter to a string
//Parsing integer was not passing correctly.

clientID.setText(ID);

In the onclick:

//Once all the conditions are met pass the clientID for this instance.

SharedPreferences.Editor editor = prefs.edit(); //USING EDITOR TO ADD TO THE PREFERENCES
editor.putInt("ID", c);

....// then other stuff.

Any thoughts? My apologies if this is a noob question. I just want to be sure about this approach if this app goes all the way.

Thanks

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top