首先你对c = db.query("rates_table", columns, "place1", null, null, null, null);有疑问
第三个参数将导致没有行被选中。
您可以使用 c = db.query("rates_table", columns, null, null, null, null, null); ,它将返回所有行。
或者您可以使用c = db.query("rates_table", columns, "place1 = 'myplace'", null, null, null, null);,在这种情况下,只会显示在place1 列中具有myplace 值的行。
最佳实践方法是在第三个参数中使用 ? 占位符(例如“place1=?”)和第四个参数中的相应参数(例如new String[]{"myplace"}),因此要复制上一个查询,您可以使用c = db.query("rates_table", columns, "place1=?", new String[]{"myplace}, null, null, null);
使用c.moveToNext,将尝试移动到光标的下一行(最初是第一行)。但是,如果它不能移动(即没有行,就像上面描述的那样),它不会失败,而是返回 false(如果光标可以移动,则返回 true)。
所以你需要检查这个,否则在没有行的情况下,尝试访问行将失败,游标越界请求索引 0,大小为 0(即你请求第一个(索引 0)当游标大小(行数)为0时。
有多种检查方法。
但是我怀疑你会想知道为什么你的循环只显示 1 列。那是因为您在查询中说过只获取 1 列。
如果您将查询的第二个参数更改为 null,它将获取所有列。
您想返回一个包含所有地点的数组。
那么假设:-
// get Cursor with all rows(3rd parm null) for the place1 column (2nd parm)
c = db.query("rates_table", columns, null, null, null, null, null);
// Create String array according to the number of rows returned.
String[] places = new String[c.getCount()];
// loop through all rows setting the respective places element with the
// value obtained from the Cursor
while (c.moveToNext) {
places[c.getPosition()] = csr.getString(csr.getColumnIndex("place1"));
}
csr.close(); // Should always close a Cursor
return places;