Difficult to make an Android Sqlite query

Asked

Viewed 143 times

0

I’m doing a project for college but I’ve been having difficulties with consultations to Sqlite. I wish you could assist me in this issue.

Oncreate:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chamado);

    buscarDados();
    criaLista();

}

search data:

public void buscarDados(){
     try{
        db = openOrCreateDatabase("cadastro",Context.MODE_ENABLE_WRITE_AHEAD_LOGGING,null);
        cursor = db.rawQuery("select * from cadastro",null);

        Toast.makeText(getApplicationContext(), "Dados encontrados", Toast.LENGTH_LONG).show();
     }catch (Exception e){
        Toast.makeText(getApplicationContext(), "Erro", Toast.LENGTH_LONG).show();
     }
}

creatureList:

public void criaLista(){
    lista = (ListView) findViewById(R.id.lista);

    String[] dados = {"titulo"};
    int[] to = {R.id.lista};

    adapter = new SimpleCursorAdapter(getApplicationContext(),R.layout.activity_chamado,cursor,dados,to);

    lista.setAdapter(adapter);
}
  • 1

    What difficulty are you having?

  • Simple My query never works always returns error.

  • Felipe, what error does the query return? My first suggestion is to use an Asynctask or Cursorloader to make your query and popular your Adapter (the first is simpler), because it is making disk access on Main Thread, which is "forbidden" (you can do, but it is not a good practice).

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.