如何从数据库中检索位置对象?
我想将 Location 对象存储到我的数据库中,然后能够加载它们。如何根据 Location 对象的属性构建 Location 对象?
- 我应该创建一个空白位置然后设置属性吗? (如果是,如何?)
- 我应该使用我的数据库作为 LocationProvider 吗? (如果是,怎么做?)
谢谢!
编辑:到目前为止,我发现的唯一可行的解决方案是检索当前位置,并覆盖所有属性......必须有更好的方法!
我的代码:
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;
}
I want to store Location objects into my database, and then be able to load them. How can I build a Location object from its attributes?
- Should I create a blank Location and then set the attributes? (If yes, how?)
- Should I use my database as a LocationProvider? (If yes, how?)
Thanks!
EDIT: The only working solution I found so far is to retrieve the current Location, and override all the attributes... There has to be a better way!
my code:
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是有道理的。我相信您可以使用
这样您就不必获取当前位置。这仍然应该创建一个新的位置,但提供者字符串为 null,使用其他位置构造函数:
请参阅 http://developer.android.com/reference/android/location/Location.html
This makes sense. I believe you could use
So that you don't have to get your current location. This should still create a new Location, but with a provider string of null, using the other Location constructor:
See http://developer.android.com/reference/android/location/Location.html