mercredi 9 décembre 2015

Save userdefined object (GPS Location) in SQLite Database

I am developing a App which recives GPS Locations. The double Latitude and the double Longitude will be passed in a own definded class -MyLocation.If the location fullfilld the requirments it will be stored in a List >MyLocation<.

After that I want to store this List in a SQLite Database.

static class Helper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "database";
    private static final String TABLE_NAME = "DENNISTABLE";
    private static final int DATABASE_VERSION = 1;
    private static final String UID = "_id";
    private static final String NAME = "Name";
    private static final String OVERALLTIME = "test";
    private static final String BESTROUND = "Password";
    private static final String LOCATIONLIST = "LocationList";
    private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + UID
            + " INTEGER PRIMARY KEY AUTOINCREMENT," + NAME + " VARCHAR(255)," + BESTROUND + " VARCHAR(255),"
            + OVERALLTIME + " VARCHAR(255)," + LOCATIONLIST + "VARCHAR(255));";
    private static final String DROP_TABLE = "DROP TABLEIF EXISTS" + TABLE_NAME;
    private Context context;

    public Helper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.context = context;
        Message.message(context, "contructor called");
    }

This is part of my code where I request the list from my database.

    public List<ArrayList<MyLocation>> getLocationList(){
    SQLiteDatabase db = helper.getWritableDatabase();
    String[] columns= { DennisHelper.UID, DennisHelper.NAME, DennisHelper.BESTROUND, DennisHelper.OVERALLTIME };
    Cursor cursor = db.query(DennisHelper.TABLE_NAME, columns, null, null, null, null, null);
    // List<MyLocation> mLocList = new ArrayList<MyLocation>();
    List<ArrayList<MyLocation>> LocationLists = new ArrayList<ArrayList<MyLocation>>();
    List<MyLocation> LocationList = null;
    while(cursor.moveToNext()){
        LocationList = cursor.get??????
    }

    return LocationLists;
}

But curser does not know the object List. I read about serialization or about using db40. I think this is a common problem. So are there best practices ways to request user defined objects by using a SQLite Database???

Any help is appreciated. Thanks

Aucun commentaire:

Enregistrer un commentaire