【发布时间】:2017-02-13 19:17:54
【问题描述】:
我想将一个类对象保存到 db。我使用了 Serializable 但我的类中的一个对象始终为空。
import com.google.android.gms.maps.model.LatLng;
import java.io.Serializable;
import java.util.ArrayList;
public class A implements Serializable {
protected String Name;
protected int Type;
protected transient List<LatLng> Nodes=new ArrayList<LatLng>();
}
nodes 始终为空。如果我删除“瞬态”,那么所有对象都变为空。
保存到db是这样的:
public long InsertList(A Data) {
byte[] bytes = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(Data);
oos.flush();
oos.close();
bos.close();
bytes = bos.toByteArray();
} catch (IOException ex) {
//TODO: Handle the exception
ex.printStackTrace();
;
}
ContentValues insertValues = new ContentValues();
insertValues.put("Data", bytes);
insertValues.put("Selected", 1);
return mDb.insert("Table", null, insertValues);
}
【问题讨论】:
-
为什么不迁移到 Realm 数据库?
-
@MohammedAtif 我对 sqlite 没有任何问题。我的主要问题是节点对象始终为空。
-
请说明如何将对象保存到数据库中。
标签: android database serializable