How can I store an integer two-dimensional array in SharedPreferences? - android

I have an integer two-dimensional array and I want to store it in SharedPreferences. Could someone show me sample code how to do that? I suppose it is possible if I will convert it to string but it works for one-dimensional integer array. Maybe other way is exist to do that? Thanks.

As you said, converting to string that two-dimensional array will work. However if you face the same problem with more complex objects, you can always convert it to string using the GSON library.
Download: Link
Another post: Link
If you want to pass a two-dimensional array to string, you can iterate through it while storing it to a StringBuffer, as this example:
StringBuffer results = new StringBuffer();
String separator = ","
float[][] values = new float[50][50];
// init values
for (int i = 0; i < values.length; ++i)
{
result.append('[');
for (int j = 0; j < values[i].length; ++j)
if (j > 0)
result.append(values[i][j]);
else
result.append(values[i][j]).append(separator);
result.append(']');
}
result.toString(); //<- Save this in your preference

Related

get JSONArray data inside JSONArray

I need to get "examples" data that is an array inside of 'results'
JSON FILE
and append it to a string. What would be the easiest way to do that?
this is how i would get "definitions"
private JSONObject queryResults;
...
...
String finalResults = "";
JSONArray results = queryResults.getJSONArray("results");
for(int i = 0; i < results.length(); i++){
JSONObject item = results.getJSONObject(i);
finalResults += item.getString("definition") + " ";
}
As this topic is related to Android application, the easiest way to get the mentioned value without using any additional libraries is to check if JSONObject inside the for-loop has attribute definitions and get the value as a String.
You can go with it like this
String finalExamples = "";
JSONArray results = queryResults.getJSONArray("results");
for (int i=0; i < results.length(), i++) {
JSONObject item = results.getJSONObject(i);
if (item.has("examples")) {
examples += item.getString("examples");
}
}
I assumed that you don't want to update your result String value if examples is not available in the JSONObject.
If you want to handle other cases, for example when "examples" is available in the object but is null or is set to empty String you can use other methods of JSONObject to check it.
More info about working with JSONObjects you can find in the documentation

How to pass edit text values to integer array in Android

I'm a beginner in Android.
I have created a edit text field where can I enter values from 0-9. Now, I have to get these values in an integer array.
example : entered values are 12345.
I need an array containing these values
int[] array = {1,2,3,4,5};
I need to know the way to do this. Kindly help.
Try this :
String str = edittext.getText.toString();
int length = str.length();
int[] arr = new int[length];
for(int i=0;i<length;i++) {
arr[i] = Character.getNumericValue(str.charAt(i));
}
You can use something like this:
int[] array = new int[yourString.length()];
for (int i = 0; i < yourString.length(); i++){
array[i] = Character.getNumericValue(yourString.charAt(i));
}

How to store array values of type String[] and BigInteger[] into shared preference inside for loop

I am receiving the following array values in a for loop:
String[] array1 = new String[""];
BigInteger array2 = new BigInteger[10];
for (int i = 0; i <= count; i++) {
array1[i] //of type string array
array2[i] //of type bigint array
//Now inside same loop i want to store and retrieve those values of array
from shared preferences. Can someone tell me how to store values of array
into preference which are of type String[] and BigInteger[]
}
If you want to store whole array data in shared preferences then you need to take shared preferences key array as same size of your array.
OR
You can append all array data with comma separator in one string object & store in shared preferences and when you get data then split string by comma.
Updated
String[] array1 = new String[10];
StringBuilder array1Data = new StringBuilder();
for (int i = 0; i <= count; i++) {
array1Data.append(array1[i]);
array1Data.append(",");
}
setSharedPreferences("KEY", array1Data.toString()); // Custom method
}
You can use JSONArray array for that. For insert value call array.put(String) or array.put(long). Insert into preferences as String by calling array.toString(). Get from preferences - array = new JSONArray(stringFromPreferences). Get value from array - array.getString(position) and array.getLong(postion).
You can also work with array.put(Object) and (String) array.get(position).

Android - moving char array value to same char array

how am I going to move the value of a char array to the same char array? Here's a code:
Assuming ctr_r1=1 ,
for(int ctr_x = (ctr_r1 + 2) ; ctr_x < letters.length - 2 ; ctr_x++)
{
letters[ctr_x] = letters[ctr_x];
}
sb.append(letters);
char[] lettersr1 = sb.toString().toCharArray();
sb1.append(lettersr1);
append the "letters", then convert it to string, then convert it to char array then make it as "lettersr1" value.
what im trying to accomplish is given the word EUCHARIST, i need to take the word HARIST out and place it on another array and call it region 1 (Porter2 stemming algorithm).
The code "ctr_X = (ctr_r1 + 2)" starts with H until T. The problem is I cannot pass the value directly that's why i'm trying to update the existing char array then append it.
I tried doing this:
char[] lettersr1 = null;
for(int ctr_x = (ctr_r1 + 2) ; ctr_x < letters.length - 2 ; ctr_x++)
{
lettersr1[ctr_x] = letters[ctr_x];
}
sb.append(lettersr1);
but my app crashes when i do that. Any help please. Thanks!
I don't understand what you're trying to do, but I can comment on your code:
letters[ctr_x] = letters[ctr_x];
This is a noop: it sets an array element value to the value it already has.
char[] lettersr1 = null;
for(int ctr_x = (ctr_r1 + 2) ; ctr_x < letters.length - 2 ; ctr_x++) {
lettersr1[ctr_x] = letters[ctr_x];
This obviously causes a NullPointerException, since you're trying to access an array which is null. You must initialize the array before being able to modify it:
char[] lettersr1 = new char[someLength];
Additional note: you should choose better names for your variables. The names should tell what the variable represents, and they should respect the Java naming conventions (no underscores in variable names, camelCase). ctr_x, ctr_r1 and lettersr1 don't mean anything.
EDIT:
I'm still not sure what you want to do, and why you don't simply use substring(), but here's how to transform EUCHARIST to HARIST:
char[] eucharist = "EUCHARIST".toCharArray();
char[] harist = new char[6];
System.arraycopy(eucharist, 3, harist, 0, 6);
String haristAsString = new String(harist);
System.out.println(haristAsString);
// or
char[] harist2 = new char[6];
for (int i = 0; i < 6; i++) {
harist2[i] = eucharist[i + 3];
}
String harist2AsString = new String(harist2);
System.out.println(harist2AsString);
// or
String harist3AsString = "EUCHARIST".substring(3);
char[] harist3 = harist3AsString.toCharArray();
System.out.println(harist3AsString);
May be so:
String str = "EUCHARIST";
str = str.substring(3);
and after toCharArray() or smth another

BLOB to String[][]

Is there a way to directly convert a "BLOB"-array into a String[][]-array (on Android?
Thanks very much in advance!
From what I understand, blobs return bytes. So you want to build a byte array and then build the string from that. This is an example.
Hope it gives you a good start.
Try this:
Converting blob array to string array:
BLOB[] blobs = getBlobs(); //fetch it somehow
String[] strings = new String[blobs.length];
for(int i = 0; i < blobs.length; i++)
strings[i] = new String(blobs[i].getBytes(0, blobs[i].length());
return strings;
Converting blob array to string matrix:
BLOB[] blobs = getBlobs(); //fetch it somehow
String[][] strings = new String[blobs.length][];
for(int i = 0; i < blobs.length; i++)
strings[i] = blobToStringArray(blobs[i]);
return strings;
My solution doesn't require codingBLOBs to String[][]-arrays anymore, now I am (de-)coding SoapEnvelopes to byte-arrays and vice versa using a method described here: http://androiddevblog.blogspot.com/2010/04/serializing-and-parceling-ksoap2.html

Categories

Resources