我的SQLite数据库中有5个字段。我想只检索3个字段(id,纬度,经度)来计算与当前位置的相应距离。我是Android编程的初学者,不确定代码。
public class Camera {
// Labels Table Columns names
public static final String KEY_CID = "id";
public static final String KEY_cname = "cameraname";
public static final String KEY_districtid = "district_id";
public static final String KEY_speedlimit = "Speedlimit";
public static final String KEY_lat = "latitude";
public static final String KEY_lng = "longitude";
public int camera_ID;
public String camera_name;
public int district_ID;
public int speedlimit;
public long clat;
public long clng;
//Also contains setters and getters}
以下是我检索纬度和经度的方法:
public ArrayList< String> getLatLngList(){
SQLiteDatabase db = dbOpenerCam.getReadableDatabase();
String query = "SELECT " +
Camera.KEY_CID + "," +
Camera.KEY_lat + "," +
Camera.KEY_lng +
" FROM " + Camera.TABLE;
ArrayList<String> latLngList = new ArrayList<String>();
Cursor cursor = db.rawQuery(query,null);
if (cursor.moveToFirst()) {
do {
ArrayList<String> camera= new ArrayList<String>();
camera.add(cursor.getString(cursor.getColumnIndex(Camera.KEY_CID)));
camera.add(cursor.getString(cursor.getColumnIndex(Camera.KEY_lat)));
camera.add(cursor.getString(cursor.getColumnIndex(Camera.KEY_lng)));
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return latLngList;
}
我如何才能访问纬度和经度来执行计算? 谢谢:))
以下是数据库创建的代码:
public class DBOpenerCam extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "camera2.db";
public DBOpenerCam(Context context ) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE_STUDENT = "CREATE TABLE " + Camera.TABLE + "("
+ Camera.KEY_CID + " INTEGER PRIMARY KEY AUTOINCREMENT ,"
+ Camera.KEY_cname + " TEXT, "
+ Camera.KEY_districtid + " TEXT, "
+ Camera.KEY_speedlimit + " TEXT, "
+ Camera.KEY_lat + " DECIMAL(7,5), "
+ Camera.KEY_lng + " DECIMAL(7,5))";
db.execSQL(CREATE_TABLE_STUDENT);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + Camera.TABLE);
// Create tables again
onCreate(db);
}
}
答案 0 :(得分:0)
你有带有getter和setter的Camera类,你可以使用同一个类从数据库中检索你的数据。
public ArrayList< Camera> getLatLngList(){
ArrayList<Camera> cameraList= new ArrayList<Camera>();
Cursor cursor = db.rawQuery(query,null);
if (cursor.moveToFirst()) {
do {
Camera camera = new Camera();
camera.setKey(cursor.getString(cursor.getColumnIndex(Camera.KEY_CID)));
camera.setLat(cursor.getString(cursor.getColumnIndex(Camera.KEY_lat)));
camera.setLon(cursor.getString(cursor.getColumnIndex(Camera.KEY_lng)));
cameraList.add(camera);
} while (cursor.moveToNext());
}
return cameraList;
}
使用cameraList arraylist进一步使用。希望这能帮助您实现目标。
答案 1 :(得分:0)
您犯的第一个错误是您为lat / long插入双值并尝试检索String。下面是代码,它将执行技巧并返回LatLongType对象的ArrayList(或者您可以使用您的Camera类):
public ArrayList<LatLong> getLatLong(){
SQLiteDatabase db=this.getReadableDatabase();
String query = "SELECT " +
Camera.KEY_CID + "," +
Camera.KEY_lat + "," +
Camera.KEY_lng +
" FROM " + Camera.TABLE;
Cursor cursor = db.rawQuery(query,null);
ArrayList<LatLong> result = new ArrayList<>();
if (cursor.moveToFirst()) {
do {
result.add(new LatLong(cursor.getDouble(cursor.getColumnIndex(Camera.KEY_lat)), cursor.getDouble(cursor.getColumnIndex(Camera.KEY_lng))));
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return result;
}
这是LatLong课程:
public class LatLong {
private double latitude;
private double longitude;
public LatLong(double latitude, double longitude) {
this.latitude = latitude;
this.longitude = longitude;
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
}
以下是Camera类的解决方案:
public ArrayList<Camera> getLatLong(){
SQLiteDatabase db=this.getReadableDatabase();
String query = "SELECT " +
Camera.KEY_CID + "," +
Camera.KEY_lat + "," +
Camera.KEY_lng +
" FROM " + Camera.TABLE;
Cursor cursor = db.rawQuery(query,null);
ArrayList<Camera> result = new ArrayList<>();
if (cursor.moveToFirst()) {
do {
Camera c = new Camera();
c.setClat((long)cursor.getDouble(cursor.getColumnIndex(Camera.KEY_lat)));
c.setClng((long)cursor.getDouble(cursor.getColumnIndex(Camera.KEY_lng)));
result.add(c);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return result;
}