我想将位置对象存储到数据库中,然后能够加载它们。如何从其属性构建位置对象?
-
我应该创建一个空白位置然后设置属性吗?(如果是,怎么做?)
-
我应该将我的数据库用作位置提供程序吗?(如果是,怎么做?)
谢谢!
编辑:到目前为止,我找到的唯一解决方案是检索当前位置,并覆盖所有属性。。。一定有更好的办法!
我的代码:
public Location selectLocationById(int id){
String query = "SELECT * FROM " + LOCATIONS_TABLE + " WHERE id = " + id + ";";
Cursor c = db.rawQuery(query, null);
Location l = null;
if(c.getCount() == 1) {
c.moveToFirst();
if(c.getColumnCount() == 8){
l = new Location(MyProject.getCurrentLoc());
l.setAccuracy(c.getFloat(1));
l.setAltitude(c.getDouble(2));
l.setBearing(c.getFloat(3));
l.setLatitude(c.getDouble(4));
l.setLongitude(c.getDouble(5));
l.setSpeed(c.getFloat(6));
l.setTime(c.getLong(7));
}
}
if(c != null && !c.isClosed()) {
c.close();
}
return l;
}