serialization - how to store 2dimensional array in shared preferences in android or serialize it -
I'm developing an Android app where I have to save dates (as provided in the array) I am doing this using my shared preferences, my code is working fine, but it behaves differently which I do not know why a. & Gt; When I am saving the array in the shared arrangements, then it is just as I want but B. & Gt; When it comes to receiving the data, it keeps a date, if the date array is twice before. 24 My array is 2 times but for me to show this, a single 24c. & Gt; It also gives me unordered representation of the array. I do not know why this is happening and how to solve it. Someone had asked me to do the serialization but I do not know how to do this? ** string [] [] my_date; My_date = new string [] {{"14", "26"}, {"12", "16", "24", "27"}, {"17"}, {"8", "13 {"15", "18"}, {}, {"2", "3", "6", "", "18"}, {"14"}, {}, {"29"}, {" 8 "," 23 "}, {" 4 "," 6 "," 24 "}, {}}; / * Storing data in shared priorities / / shared presentation pref = arg0.getSharedPreferences (" MyPref ", 0); // - For Private Mode, SharedPreferences.Editor Editor = pref.edit (); Set & lt; string & gt; set = new hashset & lt; string & gt; (); for int i = 0; i & lt; my_date.length; i ++) {for (int j = 0; j & lt; my_date [i] length; j ++) {set.add ("'" + my_date [i ] [J] + "'"); Log.v ("Date", my_date [i] [j]);} Editor.putStringSet ("Date", Set); editor.commit (); / * Save Fatching Deleted data * / set = pref.getStringSet ("date", set); Log.v ("date", set + "----" + set.size ()); First of all: This is a bad idea. You probably want to store things in a way so that you are part of them. Editing, e.g. Such as using your own custom SQLLite database. But if you are okay with the hacki approach, and you want to store that multi-dimension array in the shared preferences, then maybe just serial it in a string and store the string stored in a shared prefix. Please.
How can you serial this string, borrow bits from this example:
Take it and store that string and it in the shared prefix.
Edit: Oh okay, I was curious so I tried to get it out. I was actually found that I had to change the base 64 sign language well , And I think that I have to explain why in a compositing manner, but what I have done roughly: private zero mySharedPreferencesThing () {string [] [] = first New string [] [] {{"1", "2", "3"}, {}, {"4", "5", "6"}}; System.out.println ("String thing before:" + Syntestostring (first)); Shared References SP = getSharedPreferences ("lolcats", MODE_PRIVATE); String [] [] = after tap; try {sp.edit () PutString ("cats", two-dimensional stringorrestostring (first)). Committed (); After = Strigetovo Dormitory StringArere (sp.getString ("cats", faucet)); } Hold (exception e) {e.printStackTrace (); } System.out.println ("After the string chief:" + Erat toastring (later)); } // Just for debugging and knowing that it looks correct - maybe // is created to do some personal string string arrays (string [] [] arr {StringBuilder sb = new StringBuilder (); Sb.append ("{\ n"); For (int i = 0; i & lt; arr.length; i ++) {sb.append ("\ t {" + TextUtils.join (",", IR [i]) + "}"); If (i! = Arr.length - 1) {sb.append (","); } Sb.append ("\ n"); } Sb.append ("}"); Return sb.toString (); } Private string Two-dimensional stringArrayString (string [] [] s) Unsupported encoding exception, IOException {Bitereutuptreme bo = null; ObjectOutputStream = Zero; Base64OutputStream b64 = Faucet; Try {bo = new byteoutputstream (); B64 = New Base64OutputStream (bo, Base64.DEFAULT); Then = new object overputstream (B64); so.writeObject (s); Return bo.toString ("UTF-8"); } Finally {if (bo! = Null) {bo.close (); } If (b64! = Null) {b64.close (); } If (then! = Null) {so.close (); }}} Private string [] [] stringToTwoDimensionalStringArray throws ClassNotFoundException (I); IOException {ByteArrayInputStream bi = null; ObjectInputStream si = Faucet; Base64InputStream b64 = Faucet; Try {Byte B [] = s.getBytes ("UTF-8"); Bi = new byteforest instream (b); B64 = New Base 64 InputStream (Bye, Base 64. Default); C = New Object InStream (B64); Return (string [] []) si.readObject (); } Finally {if (bi! = Null) {bi.close (); } If (b64! = Null) {b64.close (); } If (c! = Null) {si.close (); }}}
Comments
Post a Comment