3
votes

I can't seem to figure out why serialization saves and restores the list of objects, but not the their state. The list is displayed, but not the title which is contained in the object. The object class implements Serializable.

Serialization of objects ("c"):

arrayList.add ( c );
    String fileName = "testFile";

    try {
        FileOutputStream fos = this.openFileOutput ( fileName, Context.MODE_PRIVATE );
        ObjectOutputStream os = new ObjectOutputStream ( fos );
        os.writeObject ( arrayList );
        fos.close ();
        os.close ();
    } catch ( Exception ex ) {
        ex.printStackTrace ();
    }
}

Deserialization:

    FileInputStream fis = this.openFileInput ( fileName );
        ObjectInputStream ois = new ObjectInputStream ( fis );
        arrayList = ( ArrayList<TestObject> ) ois.readObject ();
        ois.close ();
        return arrayList;

Adding objects to adapter:

    for ( TestObject c : arrayList ) {
        adapter.add ( c );
    }

Edit: part of the TestObject class:

public class TestObject implements Serializable {

private String mName;

@Override
public String toString () {
    return mName;
}

public String getName () {
    return mName;
}

public void setName ( String name ) {
    mName = name;
}
1
you are writing an arraylist of string and read an arraylist of TestObjcet? - Blackbelt
I'm not following. Why would ArrayList be of type String? - abc32112
Serialization of objects ("c").. anyway what does "their state" means? - Blackbelt
to me looks good. Are you sure the issue is not related with the adpater? - Blackbelt
I'm at a loss, but if I set an initialize the instance variables in the object class, they're displayed correctly, so that seems to implicate the save code. - abc32112

1 Answers

0
votes

yes, It is also working for me check

public class SerializationIssue {
private static final String fileName = "testFile";

public static void main(String[] args) {
    TestObject object1= new TestObject();
    TestObject object2=new TestObject();
    object1.setName("object1");
    object2.setName("object2");

    List<TestObject> list=new ArrayList<TestObject>();
    list.add(object1);
    list.add(object2);

    serializeList(list);
    ArrayList<TestObject> deserializedList=desializeDemo();
    System.out.println(deserializedList.get(0).getName());

}

private static ArrayList desializeDemo() {
    ArrayList<TestObject> deserializedList;
     try
      {
         FileInputStream fileIn = new FileInputStream(fileName);
         ObjectInputStream in = new ObjectInputStream(fileIn);
         deserializedList= (ArrayList<TestObject>) in.readObject();
         in.close();
         fileIn.close();
      }catch(IOException i)
      {
         i.printStackTrace();
         return null;
      }catch(ClassNotFoundException c)
      {
         System.out.println("Employee class not found");
         c.printStackTrace();
         return null;
      }
    return deserializedList;
}

private static void serializeList(List<TestObject> list) {
    // TODO Auto-generated method stub

        try {
            FileOutputStream fos = new FileOutputStream(fileName);
            ObjectOutputStream os = new ObjectOutputStream ( fos );
            os.writeObject ( list );
            fos.close ();
            os.close ();
        } catch ( Exception ex ) {
            ex.printStackTrace ();
        }

}
}

TestObject bean

public class TestObject implements Serializable{

    /**
     * serial version.
     */
    private static final long serialVersionUID = 1L;
    String name;
    public TestObject(String name) {
        super();
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}

Output:object1