Skip to content

Commit

Permalink
CursorReader completed.
Browse files Browse the repository at this point in the history
  • Loading branch information
ShinChven committed Feb 5, 2017
1 parent c472d7a commit 9e0d137
Showing 1 changed file with 41 additions and 20 deletions.
61 changes: 41 additions & 20 deletions dbkits/src/main/java/com/github/shinchven/dbkits/CursorReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import android.util.Log;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

Expand All @@ -19,59 +21,73 @@ public class CursorReader {

/**
* read a cursor and reflect data to the type provided.
* @param cursor a cursor from SQLite query.
* @param type the type you with to convert your data to.
* @param <T> the type you with to convert your data to.
*
* @param cursor a cursor from SQLite query.
* @param type the type you with to convert your data to.
* @param <T> the type you with to convert your data to.
* @param ignoredFieldNames you can ignore some fields if you provide its name.
* @return
*/
public static <T> List<T> read(Cursor cursor, Class<T> type) {
public static <T> List<T> read(Cursor cursor, Class<T> type, String... ignoredFieldNames) {
List<String> fieldsToIgnore = Arrays.asList(ignoredFieldNames);
List<T> list = new ArrayList<>();

if (cursor != null) {
while (cursor.moveToNext()) {
Object obj = null;
T obj = null;
try {
obj = type.newInstance();
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "instance creating failed: " + type.getName());
continue;
}
Class<?> clazz = obj.getClass();
Field[] fields = clazz.getFields();
Field[] fields = type.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
int columnIndex = 0;

if (fieldsToIgnore.contains(field.getName()))
continue;

if (Modifier.isStatic(field.getModifiers())) {
continue;
}

int columnIndex = -1;
try {
Object value = field.get(obj);
columnIndex = cursor.getColumnIndex(field.getName());

if (columnIndex < 0) {
Log.i(TAG, "field: " + field.getName() + " matches no column in cursor.");
continue;
}

columnIndex = cursor.getColumnIndexOrThrow(field.getName());
if (value instanceof Integer) {
if (field.getType() == Integer.class || field.getType().getName().equals("int")) {
int data = cursor.getInt(columnIndex);
field.setInt(obj, data);
} else if (value instanceof Double) {
} else if (field.getType() == Double.class || field.getType().getName().equals("double")) {
double data = cursor.getDouble(columnIndex);
field.setDouble(obj, data);
} else if (value instanceof Float) {
} else if (field.getType() == Float.class || field.getType().getName().equals("float")) {
float data = cursor.getFloat(columnIndex);
field.setFloat(obj, data);
} else if (value instanceof Long) {
} else if (field.getType() == Long.class || field.getType().getName().equals("long")) {
long data = cursor.getLong(columnIndex);
field.setLong(obj, data);
} else if (value instanceof String) {
} else if (field.getType() == String.class) {
String data = cursor.getString(columnIndex);
field.set(obj, data);
} else if (value instanceof Boolean) {
} else if (field.getType() == Boolean.class || field.getType().getName().equals("boolean")) {
boolean data = cursor.getInt(columnIndex) > 0;
field.setBoolean(obj, data);
} else if (value instanceof Date) {
} else if (field.getType() == Date.class) {
long raw = cursor.getLong(columnIndex);
Date date = new Date(raw);
field.set(obj, date);
} else if(value instanceof Uri){
} else if (field.getType() == Uri.class) {
String uriString = cursor.getString(columnIndex);
Uri uri = Uri.parse(uriString);
field.set(obj,uri);
field.set(obj, uri);
} else {
Log.i(TAG, "field: " + field.getName() + " not filled");
}
Expand All @@ -82,7 +98,11 @@ public static <T> List<T> read(Cursor cursor, Class<T> type) {


}
list.add(((T) obj));
try {
list.add(((T) obj));
} catch (Exception e) {
e.printStackTrace();
}

}
}
Expand All @@ -91,4 +111,5 @@ public static <T> List<T> read(Cursor cursor, Class<T> type) {
}



}

0 comments on commit 9e0d137

Please sign in to comment.