Created
July 25, 2017 00:52
-
-
Save tiagolpadua/3d3d881cdf923a5d3ebf6ec049fb4da6 to your computer and use it in GitHub Desktop.
FJ-57 Ex 4.9 Item 9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.caelum.cadastro.dao; | |
import android.content.ContentValues; | |
import android.content.Context; | |
import android.database.Cursor; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.database.sqlite.SQLiteOpenHelper; | |
import java.lang.reflect.Field; | |
import java.util.ArrayList; | |
import java.util.List; | |
import br.com.caelum.cadastro.modelo.Aluno; | |
public class AlunoDAO extends SQLiteOpenHelper { | |
private static final int VERSAO = 1; | |
private static final String TABELA = "Alunos"; | |
private static final String DATABASE = "CadastroCaelum"; | |
public AlunoDAO(Context context) { | |
super(context, DATABASE, null, VERSAO); | |
} | |
@Override | |
public void onCreate(SQLiteDatabase database) { | |
String ddl = "CREATE TABLE " + TABELA | |
+ " (id INTEGER PRIMARY KEY, " | |
+ " nome TEXT NOT NULL, " | |
+ " telefone TEXT, " | |
+ " endereco TEXT, " | |
+ " site TEXT, " | |
+ " nota REAL);"; | |
database.execSQL(ddl); | |
} | |
@Override | |
public void onUpgrade(SQLiteDatabase database, | |
int versaoAntiga, int versaoNova) { | |
String sql = "DROP TABLE IF EXISTS " + TABELA; | |
database.execSQL(sql); | |
onCreate(database); | |
} | |
public void insere(Aluno aluno) { | |
ContentValues values = new ContentValues(); | |
values.put("nome", aluno.getNome()); | |
values.put("telefone", aluno.getTelefone()); | |
values.put("endereco", aluno.getEndereco()); | |
values.put("site", aluno.getSite()); | |
values.put("nota", aluno.getNota()); | |
getWritableDatabase().insert(TABELA, null, values); | |
} | |
public List<Aluno> getLista() { | |
List<Aluno> alunos = new ArrayList<Aluno>(); | |
SQLiteDatabase db = getReadableDatabase(); | |
Cursor c = db.rawQuery("SELECT * FROM " + TABELA + ";", null); | |
while (c.moveToNext()) { | |
Aluno aluno = new Aluno(); | |
// aluno.setId(c.getLong(c.getColumnIndex("id"))); | |
// aluno.setNome(c.getString(c.getColumnIndex("nome"))); | |
// aluno.setTelefone(c.getString(c.getColumnIndex("telefone"))); | |
// aluno.setEndereco(c.getString(c.getColumnIndex("endereco"))); | |
// aluno.setSite(c.getString(c.getColumnIndex("site"))); | |
// aluno.setNota(c.getDouble(c.getColumnIndex("nota"))); | |
for (String field : c.getColumnNames()) { | |
set(aluno, field, c); | |
} | |
alunos.add(aluno); | |
} | |
c.close(); | |
return alunos; | |
} | |
private boolean set(Object object, String fieldName, Cursor c) { | |
Class<?> clazz = object.getClass(); | |
while (clazz != null) { | |
try { | |
Field field = clazz.getDeclaredField(fieldName); | |
field.setAccessible(true); | |
if(field.getType().isAssignableFrom(Integer.class)) { | |
field.set(object, c.getInt(c.getColumnIndex(fieldName))); | |
} else if(field.getType().isAssignableFrom(Long.class)) { | |
field.set(object, c.getLong(c.getColumnIndex(fieldName))); | |
} else if(field.getType().isAssignableFrom(String.class)) { | |
field.set(object, c.getString(c.getColumnIndex(fieldName))); | |
} else if(field.getType().isAssignableFrom(Double.class)) { | |
field.set(object, c.getDouble(c.getColumnIndex(fieldName))); | |
} | |
return true; | |
} catch (NoSuchFieldException e) { | |
clazz = clazz.getSuperclass(); | |
} catch (Exception e) { | |
throw new IllegalStateException(e); | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment