display listview in the same Activity after consultation

Asked

Viewed 601 times

0

I have this consultation layout with a Listview:

inserir a descrição da imagem aqui

After marking the RadioButton and write the search parameter would like the list to appear with the database data (detail, I have a Listview that returns all data registered in the database working normally), but I click on search and nothing happens, nor return of errors, I am using the same adapter from my other list that is working normally.

Does the problem is because the listview in the same Activity of the consultation?

My Consulting Activity:

public class ConsultaActivity extends AppCompatActivity {
private Livro livro = new Livro();
private RadioGroup radioGroup;
private EditText editText;
private ListView lvConsulta;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_consulta);

    radioGroup = (RadioGroup) findViewById(R.id.rdgOpcoes);
    editText = (EditText) findViewById(R.id.edtconsulta);
    lvConsulta = (ListView) findViewById(R.id.lvConsulta);
}

public void consultar(View view) {
    LivroCRUD livroCRUD = new LivroCRUD(this);
    try {
        switch (radioGroup.getCheckedRadioButtonId()) {
            case R.id.rbtnTitulo:
                    livro.setTitulo(editText.getText().toString());
                    List<Livro> listaTitulo = livroCRUD.buscarTitulo(livro);
                    lvConsulta.setAdapter(new LivroAdapter(this, listaTitulo));
              Toast.makeText(this, "passou pelo processo......", Toast.LENGTH_SHORT).show();
                break;
            case R.id.rbtnAutor:
                    livro.setAutor(editText.getText().toString());
                    List<Livro> listaAutor = livroCRUD.buscarAutor(livro);
                    lvConsulta.setAdapter(new LivroAdapter(this, listaAutor));
                break;
            case R.id.rbtnEditora:
                    livro.setEditora(editText.getText().toString());
                    List<Livro> listaEditora = livroCRUD.buscarEditora(livro);
                    lvConsulta.setAdapter(new LivroAdapter(this, listaEditora));
                break;
        }
    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(this, "Não foi possivel realizar a consulta....", Toast.LENGTH_SHORT).show();
    }
}

}

One of the search methods by parameters:

EDIT: change of method query after @ramaral tips

//BUSCAR POR TITULO
public List<Livro> buscarTitulo(String titulo) throws Exception{
    List<Livro> lista = new ArrayList<Livro>();
    String[] colunas = new String[]{"_id","titulo","autor","editora"};

    Cursor cursor = db.query("livro", colunas, "titulo= ?",new String[]{titulo},null,null,null);
    //Percorre o cursor se tiver registos
    //Se não for nulo move para o primeiro registo
    if(cursor != null && cursor.moveToFirst()){
        do {
            Livro livroTitulo = new Livro();
            //retornar os valores e adiciona na lista
            livroTitulo.setId(cursor.getInt(0));
            livroTitulo.setTitulo(cursor.getString(1));
            livroTitulo.setAutor(cursor.getString(2));
            livroTitulo.setEditora(cursor.getString(3));
            lista.add(livroTitulo);
        }while (cursor.moveToNext());
    }

    return (lista);
}

1 answer

1


No, the problem won’t be having the Listview in the same Activity.

There are "problems" in the code but, in the beginning, are no reason not to work as you want.
I say "from the start" because I don’t have an overview of him.

The "problems" I find are:

  • No reason to pass one Book to the method buscarTitulo(). He must receive a String, with the title to be searched.
  • The clause WHERE should be built using parameters
  • You must test that the stroke is not null before using it.

Search by title:

public List<Livro> buscarTitulo(String titulo) throws Exception{
    List<Livro> lista = new ArrayList<Livro>();
    String[] colunas = new String[]{"_id","titulo","autor","editora"};

    Cursor cursor = db.query("livro",
                             colunas,
                             "titulo = ?", new String[] {titulo()},
                             null, null, null);

    //Percorre o cursor se tiver registos
    //Se não for nulo move para o primeiro registo
    if(cursor != null && cursor.moveToFirst()){
        do {
            Livro livroTitulo = new Livro();
            //retornar os valores e adiciona na lista
            livroTitulo.setId(cursor.getInt(0));
            livroTitulo.setTitulo(cursor.getString(1));
            livroTitulo.setAutor(cursor.getString(2));
            livroTitulo.setEditora(cursor.getString(3));
            lista.add(livroTitulo);
        }while (cursor.moveToNext());
    }

    return lista;
}

Remember that to find a record you have to write the title as it was recorded.

Consider using MATCH instead of = to find the Title(s) (s).
The following query will bring all the books whose title begins with String passed to the method (variable titulo).

Cursor cursor = db.query("livro",
                         colunas,
                         "titulo MATCH ?", new String[] {titulo + "*"},
                         null, null, null);

Check if you have not forgotten to declare the event onClick button as the method consultar()

  • the listview still not appearing, to test I put to display a message on the screen within the switch title case, and when I press the button query the message and displayed on the screen, which I think means I went through the whole consultation process inside the case, I changed my post to show, will I have to do some operation outside the switch case?

  • 1

    See if he gets inside the block do/while of the method buscarTitulo()

  • I managed to tidy up, this displaying normally refiz the method query passing by titulo instead of titulo().

Browser other questions tagged

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