[英]Cursor should only select cells with a different value
I want, that my cursor is moving to the next cell with different content and I want the cursor to skip over all cells that have a value that already existed.我想要,我的 cursor 正在移动到具有不同内容的下一个单元格,我希望 cursor 跳过所有具有已经存在的值的单元格。
For example we have the column: Beer, Car, Car, House, Beer, Tree (in KEY_AREA)例如我们有列:Beer, Car, Car, House, Beer, Tree (in KEY_AREA)
The cursor should select: Beer, Car, House, Tree and skip 1 Car and 1 Beer. cursor 应为 select:啤酒、汽车、房屋、树木和跳过 1 辆汽车和 1 辆啤酒。
Cursor cursor = database.query(DBHelper.TABLE_AREA,
columns,
null,
null,
null,
null,
null,
null);
if (cursor.moveToFirst()) {
do {
Area_Names.add(cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.KEY_AREA)));
}
while (cursor.moveToNext());
} else
Log.d("mLog", "0 rows");
cursor.close();
Since you are interested only in the column DBHelper.KEY_AREA
you can use the overload of query()
which has as its first argument a boolean value indicating whether you want distinct results or not.由于您只对DBHelper.KEY_AREA
列感兴趣,因此您可以使用query()
的重载,它的第一个参数是 boolean 值,指示您是否想要不同的结果。
In your case you should pass true
:在您的情况下,您应该通过true
:
String[] columns = new String[] {DBHelper.KEY_AREA};
Cursor cursor = database.query(
true,
DBHelper.TABLE_AREA,
columns,
null,
null,
null,
null,
null,
null
);
Also, you don't need cursor.getColumnIndexOrThrow(DBHelper.KEY_AREA)
to get the column's index.此外,您不需要cursor.getColumnIndexOrThrow(DBHelper.KEY_AREA)
来获取列的索引。
Your query returns only 1 column and you can safely use 0
as its index:您的查询仅返回 1 列,您可以安全地使用0
作为其索引:
Area_Names.add(cursor.getString(0));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.