我似乎无法弄清楚为什么序列化会保存和恢复对象列表,而不是它们的状态。显示列表,但不显示对象中包含的标题。对象类实现 Serializable。
对象的序列化(“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 ();
}
}
反序列化:
FileInputStream fis = this.openFileInput ( fileName );
ObjectInputStream ois = new ObjectInputStream ( fis );
arrayList = ( ArrayList<TestObject> ) ois.readObject ();
ois.close ();
return arrayList;
向适配器添加对象:
for ( TestObject c : arrayList ) {
adapter.add ( c );
}
编辑:TestObject 类的一部分:
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;
}