Skip to content

Instantly share code, notes, and snippets.

@alorma
Created February 13, 2014 22:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alorma/8985595 to your computer and use it in GitHub Desktop.
Save alorma/8985595 to your computer and use it in GitHub Desktop.
Android class that improves work with Cursor.
package com.alorma.changetheworld.bbdd;
import android.content.ContentValues;
import android.database.Cursor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* Classe que ens permet treballar am els cursors.
* <br />
* Llegim cursor i el passem a K
* D'un tipus de objecte K pasem a ContentValues
*/
public abstract class BaseCursorHelper<K> {
public abstract ContentValues setValues(K k);
public ContentValues[] setValuesArray(K[] ks) {
return setValuesArray(Arrays.asList(ks));
}
public ContentValues[] setValuesArray(List<K> ks) {
ContentValues[] vs = new ContentValues[ks.size()];
for (int i = 0; i < ks.size(); i++) {
vs[i] = setValues(ks.get(i));
}
return vs;
}
public abstract K readCursor(Cursor cursor);
public List<K> readFullCursor(Cursor cursor) {
List<K> ks = new ArrayList<K>();
if (checkCursor(cursor)) {
while (cursor.moveToNext()) {
ks.add(readCursor(cursor));
}
}
return ks;
}
protected boolean checkCursor(Cursor cursor) {
return cursor != null && !cursor.isBeforeFirst() && !cursor.isAfterLast();
}
protected int columnIndex(Cursor cursor, String column) {
return cursor.getColumnIndex(column);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment