0
This is my first forum post, I hope I’m not inflicting any rules -. I’m starting in programming Android, so I’m trying to develop a CRUD. So far I made the registration and listing, but the latter insists on giving error even though the code is apparently correct... (Is consistent with the instructions of sites such as the Devmidia). I would like help to find what I am doing wrong, thanks in advance! :)
BANK CLASS CODE:
public class Banco extends SQLiteOpenHelper{
static final String NOME_BANCO = "banco.db";
static final String TABELA = "pacientes";
static final String ID = "id";
static final String PACIENTE = "paciente";
static final String LEITO = "leito";
static final int VERSAO = 1;
public Banco(Context context) { // local do banco
super(context,NOME_BANCO, null, VERSAO);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("" +
"create table pacientes(id integer primary key autoincrement, paciente text not null" +
", leito text not null);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS pacientes");
onCreate(db);
}
CRUD CODE
public class CRUD {
SQLiteDatabase db;
Banco banco;
public CRUD (Context context){
banco = new Banco(context);
}
public String insert(String paciente, String leito){
//responsável por inserir
ContentValues valores;
long resultado;
db=banco.getWritableDatabase();
//gravar no banco
valores = new ContentValues();
valores.put(Banco.PACIENTE, paciente);
valores.put(Banco.LEITO, leito);
resultado = db.insert(Banco.TABELA, null, valores);
db.close();
if (resultado ==-1)
return "Erro ao inserir registro";
else
return "Cadastrado :)";
}
public Cursor carregaDados(){
Cursor cursor;
String campos[] = {banco.PACIENTE,banco.LEITO,banco.ID};
db = banco.getReadableDatabase();
cursor = db.query(banco.TABELA, campos, null, null, null, null, null, null);
if(cursor!=null)
{
cursor.moveToFirst();
}
db.close();
return cursor;
}
CODE FOR THE DATA LISTING
public class Consulta extends AppCompatActivity {
ListView lista;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_consulta);
CRUD crud = new CRUD (getBaseContext());
Cursor cursor = crud.carregaDados();
String [] nomecampos = new String [] {Banco.PACIENTE, Banco.LEITO,Banco.ID};
int [] idViews = new int [] {R.id.idPaciente, R.id.idLeito,R.id.idId};
SimpleCursorAdapter adaptador = new SimpleCursorAdapter(getBaseContext(),
R.layout.formatador, cursor, nomecampos,idViews,0);
lista = (ListView)findViewById(R.id.listinha);
lista.setAdapter(adaptador);
}
}
What error exactly occurs? Appears somewhere? Try to describe more your problem
– Danilo de Oliveira
So friend, no explicit error appears. When I call the button that has the list method, the app simply stops working. So I did some tests, and when I delete the contents of the list method and leave the button calling only the empty Activity of the listing, the app runs normally, calling the screen and everything else. Which leads me to believe that there’s some error in the listing code itself
– Duane Gabriel
Got it. Every time you’re developing an app and eventually it stops working (and closes itself), an exception appears on logcat. If you’re using Android Studio, you’ll get a little bit in the bottom corner, called Android Monitor. When opening it, you can see debug logs, error, etc. In it should appear the exception that was launched, which would already help a lot. But it will only appear if the error occurs and you are logcat (on Android Monitor) open
– Danilo de Oliveira
So if you can update the question with this log, it will be in red...
– Danilo de Oliveira