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;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…