gpt4 book ai didi

android - 使用 Cursor 检索 SQLite 数据库

转载 作者:太空狗 更新时间:2023-10-29 16:38:33 24 4
gpt4 key购买 nike

我不认为我一直在做这件事。一位 friend 告诉我使用 cursor.getColumnIndex() 来检索该表列中项目的值。在我看来,它只是返回表中列位置的 int 值,这就是为什么当我执行下面的代码时,我的 lat 和 long 值得到 2.0 或 3.0,因为它们是中的第二列和第三列 table 。

让此方法返回经纬度列中的项目而不是它们在表格中的位置的正确方法是什么?

这是我在 getColumnIndex() 中使用的内容:

public List<LatLng> getAllPositions() {
List<LatLng> positionList = new ArrayList<LatLng>();
SQLiteDatabase database;
ResumeMySQLiteHelper dbHelper;

dbHelper = new ResumeMySQLiteHelper(this);
database = dbHelper.getWritableDatabase();

String[] allColumns = { ResumeMySQLiteHelper.COLUMN_ID,
ResumeMySQLiteHelper.COLUMN_COMMENT, ResumeMySQLiteHelper.COLUMN_LAT, ResumeMySQLiteHelper.COLUMN_LONG };

Cursor cursor = database.query(ResumeMySQLiteHelper.TABLE_NAME,
allColumns, null, null, null, null, null);

cursor.moveToFirst();
while (!cursor.isAfterLast()) {


double latitude = cursor.getColumnIndex("lat");
double longitude = cursor.getColumnIndex("long");
String testString = Double.toString(latitude);
String testLong = Double.toString(longitude);
Log.w("myApp", testString + " " + testLong);

//currently prints myApp 2.0 3.0 for every row in table

LatLng newLatLng = new LatLng(latitude, longitude);
positionList.add(newLatLng);
cursor.moveToNext();
}
// make sure to close the cursor
cursor.close();
return positionList;
}

最佳答案

这个:

double latitude = cursor.getColumnIndex("lat");

应该是:

double latitude = cursor.getDouble(cursor.getColumnIndex("lat"));

双经度也是如此

关于android - 使用 Cursor 检索 SQLite 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22152818/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com