vendredi 6 novembre 2015

Converting Cursor into generic object android

I'm implementing some funcions on SQLite for Android so I can use them for generic objects. I'm having some troubles with the "SelectAll" method. I can query my object into a cursor, having only the class of it. I'm doing this:

public ArrayList<?> SelectAll(Class<?> foo){
    Cursor cursor = database.rawQuery("select all from ?", new String[]{foo.getSimpleName()});
    ArrayList<?> list = new ArrayList<>();
    Field[] fields = foo.getFields();
    try {
        Constructor<?> constructor = foo.getConstructor(foo);
        // list.add(constructor.newInstance(  ));
    }catch(Exception ex){
        return list;
    }
    return null;
}

My problem is in the commented line. I have the array of all parameters got with the getFields. But now i don't know how to cycle them for filling my object. Anyone can help me? I need to cycle the cursor way to fill my ArrayList with foo items.

PS: This method must be generic. Not specific, so I have to get property from my Field[] and not directly hard-coding.

Thanks all in advice, any help will be appreciated.

Aucun commentaire:

Enregistrer un commentaire