Fixed some cursor-related bugs in Search.

This commit is contained in:
Greyson Parrelli 2018-05-22 10:28:12 -04:00
parent 8fc63c4d78
commit e189fff856
3 changed files with 10 additions and 6 deletions

View File

@ -30,8 +30,6 @@ public class CursorList<T> implements List<T>, Closeable {
public CursorList(@NonNull Cursor cursor, @NonNull ModelBuilder<T> modelBuilder) {
this.cursor = cursor;
this.modelBuilder = modelBuilder;
this.cursor.moveToFirst();
}
public static <T> CursorList<T> emptyList() {
@ -58,6 +56,8 @@ public class CursorList<T> implements List<T>, Closeable {
@Override
public Iterator<T> iterator() {
return new Iterator<T>() {
int index = 0;
@Override
public boolean hasNext() {
return cursor.getCount() > 0 && !cursor.isLast();
@ -65,9 +65,8 @@ public class CursorList<T> implements List<T>, Closeable {
@Override
public T next() {
T model = modelBuilder.build(cursor);
cursor.moveToNext();
return model;
cursor.moveToPosition(index++);
return modelBuilder.build(cursor);
}
};
}
@ -179,7 +178,7 @@ public class CursorList<T> implements List<T>, Closeable {
@Override
public void close() {
if (cursor != null) {
if (!cursor.isClosed()) {
cursor.close();
}
}

View File

@ -47,6 +47,7 @@ class SearchViewModel extends ViewModel {
@Override
protected void onCleared() {
debouncer.clear();
searchResult.close();
}

View File

@ -29,4 +29,8 @@ public class Debouncer {
handler.removeCallbacksAndMessages(null);
handler.postDelayed(runnable, threshold);
}
public void clear() {
handler.removeCallbacksAndMessages(null);
}
}