0

I'm using this method for converting an String array to Item array (for saving it in sharedprefrences).

      strings = new ArrayList<String>();
      for (Object object : itemArrey) {
      strings.add(object != null ? object.toString() : null);
      }
      b1s.edit().putString("array", ObjectSerializer.serialize(strings)).commit();

But for loading it in listview I need to convert it again to item array (I can't use String array for listview for some reasons). How can I achieve it?

Nabin
  • 11,216
  • 8
  • 63
  • 98
SaDeGH_F
  • 471
  • 1
  • 3
  • 14
  • Add the lines which are making problem with problem description. Your question need more code and error description. – Vaibhav Raj Aug 25 '14 at 07:53

2 Answers2

1

As Far as I Understand You need to keep "Readable Strings" for Objects and convert it back to a Object. In that case I would recommend serializing Object ArrayList to JSON directly because it has Readability(If necessary) and easy to manipulate(Encode and Decode). You can use Json-Simple or Gson to serialize but if the object is complicated its better to use Gson .

prasadmadanayake
  • 1,415
  • 15
  • 23
0

There is no general solution to this.

Basically, you want to do the reverse of what Object.toString() does ... for each object. There is no general way to do that.

If you knew what the toString() methods of items were generating, and the strings they were generating contained all of the information content of the original item objects, then it would be possible (in theory) to write a custom method to parse the strings. But you'd need to code the parse methods yourself.

The real solution is probably to serialize your ArrayList<Item> directly. If necessary, solve the problem that is preventing that from working.


You ask:

can I save serialized item array to sharedprefrences

Since you are using the ObjectSerializer class from org.apache.pig, you will be using regular Java object serialization.

That means that you can serialize an ArrayList<Item> if only if the Item class can be serialized.

And that, in turn, requires the following:

  • Item must implement Serializable (directly or indirectly) AND:
    • all non-volatile instance fields of Item must be serializable, OR
    • Item must override writeObject and readObject, OR
    • Item must also implement Externalizable.

For a field to be serializable, it must be a primitive type, a serializable object type, or an array of a serializable type. A lot of Java library types implement Serializable; e.g. the primitive wrappers, String, the Collection types, and so on.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216