我经常看到代码涉及迭代数据库查询的结果,对每一行做一些事情,然后继续前进到下一行.典型的例子如下.
Cursor cursor = db.rawQuery(...);
cursor.moveToFirst();
while (cursor.isAfterLast() == false)
{
...
cursor.moveToNext();
}
Run Code Online (Sandbox Code Playgroud)
Cursor cursor = db.rawQuery(...);
for (boolean hasItem = cursor.moveToFirst();
hasItem;
hasItem = cursor.moveToNext()) {
...
}
Run Code Online (Sandbox Code Playgroud)
Cursor cursor = db.rawQuery(...);
if (cursor.moveToFirst()) {
do {
...
} while (cursor.moveToNext());
}
Run Code Online (Sandbox Code Playgroud)
这些对我来说似乎过于冗长,每次都有多次调用Cursor
方法.当然必须有一个更简洁的方式?
Gra*_*and 510
最简单的方法是:
while (cursor.moveToNext()) {
...
}
Run Code Online (Sandbox Code Playgroud)
游标在第一个结果行之前开始,因此在第一次迭代时,如果存在,则移动到第一个结果.如果光标为空,或者已经处理了最后一行,则循环退出整齐.
当然,完成后不要忘记关闭光标,最好是在finally
子句中.
Cursor cursor = db.rawQuery(...);
try {
while (cursor.moveToNext()) {
...
}
} finally {
cursor.close();
}
Run Code Online (Sandbox Code Playgroud)
如果您的目标是API 19+,则可以使用try-with-resources.
try (Cursor cursor = db.rawQuery(...)) {
while (cursor.moveToNext()) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
Ale*_*tyl 107
我发现通过光标的最佳方式如下:
Cursor cursor;
... //fill the cursor here
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
// do what you need with the cursor here
}
Run Code Online (Sandbox Code Playgroud)
不要忘记之后关闭光标
编辑:如果你需要迭代一个你不负责的游标,给定的解决方案很棒.一个很好的例子是,如果您将光标作为方法中的参数,并且您需要扫描光标以获得给定值,而不必担心光标的当前位置.
Jör*_*eld 43
我只想指出第三种替代方案,如果光标不在起始位置也可以使用:
if (cursor.moveToFirst()) {
do {
// do what you need with the cursor here
} while (cursor.moveToNext());
}
Run Code Online (Sandbox Code Playgroud)
小智 9
如何使用foreach循环:
Cursor cursor;
for (Cursor c : CursorUtils.iterate(cursor)) {
//c.doSth()
}
Run Code Online (Sandbox Code Playgroud)
但是我的CursorUtils版本应该不那么难看,但它会自动关闭游标:
public class CursorUtils {
public static Iterable<Cursor> iterate(Cursor cursor) {
return new IterableWithObject<Cursor>(cursor) {
@Override
public Iterator<Cursor> iterator() {
return new IteratorWithObject<Cursor>(t) {
@Override
public boolean hasNext() {
t.moveToNext();
if (t.isAfterLast()) {
t.close();
return false;
}
return true;
}
@Override
public Cursor next() {
return t;
}
@Override
public void remove() {
throw new UnsupportedOperationException("CursorUtils : remove : ");
}
@Override
protected void onCreate() {
t.moveToPosition(-1);
}
};
}
};
}
private static abstract class IteratorWithObject<T> implements Iterator<T> {
protected T t;
public IteratorWithObject(T t) {
this.t = t;
this.onCreate();
}
protected abstract void onCreate();
}
private static abstract class IterableWithObject<T> implements Iterable<T> {
protected T t;
public IterableWithObject(T t) {
this.t = t;
}
}
}
Run Code Online (Sandbox Code Playgroud)
以下可能是更好的方法:
if (cursor.moveToFirst()) {
while (!cursor.isAfterLast()) {
//your code to implement
cursor.moveToNext();
}
}
cursor.close();
Run Code Online (Sandbox Code Playgroud)
上面的代码将确保它将经历整个迭代,并且不会在第一次和最后一次迭代时转义.
import java.util.Iterator;
import android.database.Cursor;
public class IterableCursor implements Iterable<Cursor>, Iterator<Cursor> {
Cursor cursor;
int toVisit;
public IterableCursor(Cursor cursor) {
this.cursor = cursor;
toVisit = cursor.getCount();
}
public Iterator<Cursor> iterator() {
cursor.moveToPosition(-1);
return this;
}
public boolean hasNext() {
return toVisit>0;
}
public Cursor next() {
// if (!hasNext()) {
// throw new NoSuchElementException();
// }
cursor.moveToNext();
toVisit--;
return cursor;
}
public void remove() {
throw new UnsupportedOperationException();
}
}
Run Code Online (Sandbox Code Playgroud)
示例代码:
static void listAllPhones(Context context) {
Cursor phones = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
for (Cursor phone : new IterableCursor(phones)) {
String name = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.d("name=" + name + " phoneNumber=" + phoneNumber);
}
phones.close();
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
124585 次 |
最近记录: |