Farzad Shahbazi
Farzad Shahbazi

Reputation: 11

Save object to sqlite db

I want to save a class object to db. I used Serializable but one of objects in my class always is null.

 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 is always null. If I remove 'transient' then all of objects become null.

Save to db is like this:

    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);

}

Upvotes: 0

Views: 2853

Answers (1)

a.r.
a.r.

Reputation: 575

The transient keyword in Java is used to indicate that a field should not be serialized.

So nodes will not be serialized in your case.

check out this Why does Java have transient fields?

Upvotes: 1

Related Questions