How to store array of integer in sharedPreferences [duplicate] - android

This question already has answers here:
Save ArrayList to SharedPreferences
(38 answers)
Closed 6 years ago.
I have an array integer in android studio like this { 0 , 0 , 0 , 1 , 1 , 0 , 1}
how can I store and recall it in shared preferences

There is no way you can store an array as it is.
Either you can convert it to JSON String and store it as a string.
Or you can store each element of the array individually with an ID like this.

Build a string out of it. Preferably using StringBuilder.
And, when reading, just split() on commas and then user Integer.parseInt() on all elements of the array.
Should be faster than the json route and does not require any extra dependency.

Related

What is the easiest way to save a variable using shared preferences in kotlin? [duplicate]

This question already has answers here:
How to use SharedPreferences in Android to store, fetch and edit values [closed]
(30 answers)
Closed 2 years ago.
What is the easiest way to save and load this variable using shared preferences (kotlin)?
var score = 12.5
And yes the score variable is a double not int.
To put inside:
with(PreferenceManager.getDefaultSharedPreferences(this).edit()){
putDouble("SCORE",score)
apply()
}
To retrieve:
val score = PreferenceManager.getDefaultSharedPreferences(this).getDouble("SCORE",0.0)

How can i store more than 24,00,000 character in sqlite? [duplicate]

This question already has an answer here:
sqlite get field with more than 2 MB [duplicate]
(1 answer)
Closed 5 years ago.
I store Base64 String in sqlite and for that I use these data types TEXT, MEDIUMTEXT and LONGTEXT. But when I cross limit of 23,00,000 character, Android is not able to read data of the table. Can anyone help me?
You can always create code to break Strings apart at specific locations, and stitch it back together if you need to read the whole string.
String string = "abc def ghi jkl mno";
String ss[] = s.split(" ", 5);
// ss = {"abc", "def", "ghi", "jkl", "mno"};

Can't match string which stored in SQLite [duplicate]

This question already has answers here:
String comparison - Android [duplicate]
(17 answers)
Closed 6 years ago.
In part of my application I get date from mobile and store it in SQLite Database as string.
when I try to select rows from database which have same date, can't find any match result.
Also as in below code, I tried to get specific cell of table which I'M sure it's match the current date. but no match.
is there any idea?
Get Date from mobile and insert it as string.
String currentDateString = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
MyDB.insertRow(currentDateString);
Check if date from database equal mobile date.
if (MyDB.getRow(21).getString(1).toString()==currentDateString)
Toast.makeText(getBaseContext(),"Match",Toast.LENGTH_SHORT).show();
You don't match Strings in Java using == operator. You use .equals() method as String is not a primitive type.
So try this -
if (MyDB.getRow(21).getString(1).toString().equals(currentDateString))
Strings aren't matched by == operator because it is an object.
try
MyDB.getRow(21).getString(1).toString().equals(currentDateString)
and if you need it not case sensitive
MyDB.getRow(21).getString(1).toString().equalsIgnoreCase(currentDateString)
Also
make sure that the inserted date format is the same that you match with

Android: how to parameterize which resource to use at runtime? [duplicate]

This question already has answers here:
Access resource string by string name in array
(2 answers)
Closed 7 years ago.
Think about a list of parameters and their respective intervals of discrete integer values:
a[1-N], b[1-M], c[1-K], d[1-J]
a, b, c, d are the variables while between square brackets there are intervals of their possible values.
At runtime if they are
a=1, b=2, c=3, d=5
then I'd like to get the resource with
name = R.string.string_1_2_3_5
Is it possible?
I wouldn't want to make a series of cascade switches for each variable to finally pick a resource. I know this could work but is there another way?
You can use Java reflection like in here.
If you need to retrieve strings like that more than once, in order to get fast access, you should first construct a hashtable with the field names of R.string as keys (maybe when you launch the app).

Parse several JSONOBject into an array in android [duplicate]

This question already has answers here:
how to parse JSONArray in android
(3 answers)
Closed 7 years ago.
From the server I get a string with exactly the following format:
[{"valueOne", "341", "valueTwo": "1432"}, {"valueOne", "6483", "valueTwo": "3267"}]
I understand that it is two JSONObject into an array, but ..
Howparse this?
My intention is to have all the concatenated string values, like this:
Strings values = (341 + 1432 + 6483 + 3267);
I guess I must first convert the string that I have received from the server to JSONObject, but do not know how to continue.
In this example there are two JSONObjects, but sometimes may contain three or more.
Many times I get values from JSONObjects values, but I have never seen in this case. I searched for information but can not find a solution that is useful to me.
I appreciate the help
greetings!
JsonArray jArray= <your parsed array>;
for(int i=0;i<=jArray.lenght()-1;i++)
{
String valueOne=jArray.getJsonObject(i).getString("ValueOne");
String valueTwo=jArray.getJsonObject(i).getString("ValueTwo");
}
you can do whatever you want with the values.

Categories

Resources