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

View File

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

View File

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