SQLITE cursor taking the last value

0

How do I make the SQLITE Cursor, cause the variables not to stop at the last value? I made a SetAdapter to which it lists all the data in the ListView, so that's fine, but when I put some variable inside the WHILE, it sets the last value, and I need it to show me all the values in the Log. i (); Below the code:

//Classe do select do SQLITE

public class Testesqlite extends Tela{

    static String testenome,testesobrenome,testenascimento;

    public void gambi(){

        Cursor c = bancosqlite.rawQuery("SELECT nome,sobrenome,nascimento FROM tabelateste",null);

        while (c.moveToNext()) {

            testenome = c.getString(c.getColumnIndex("nome")).trim();
             testesobrenome = c.getString(c.getColumnIndex("sobrenome")).trim();
             testenascimento = c.getString(c.getColumnIndex("nascimento")).trim();

        }
    }
}

  //Ação ao clicar em um item do ListView
    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int posicao, long id) {
            posi = parent.getItemAtPosition(posicao).toString();
                Testesqlite gambb = new Testesqlite();
            gambb.gambi();

                if (posi.equals(testenome)) {

                    Intent teste = new Intent(Testing.this, Tela_teste.class);
                    startActivity(teste);

                }
                 //Aqui ele me mostra corretamente a posição
                 //do item clicado, só que o da váriavel "testenome" que
                 //fica dentro do while fica só no
                 //no ultimo, fica toda hora printando o ultimo
                 //e não outros valores ; / 
                Log.i("Clicado", posi + "\t" + testenome);
            }

    });

}

The first name is the variable "posi" and the second is the "testenome" which always shows me the last value, I do not know why.

NOTE: When you click on the ListView item containing the name "Test", it opens another activity without problems, but others do nothing.

Message in logcat

  

12-08 12: 18: 24,680 26825-26825 / br.com.kappauni.teste I / Clicado: Diego Kappaun Testeaaa   12-08 12: 18: 29,960 26825-26825 / br.com.kappauni.teste I / Clicado: Diella heckler granella Testeaaa   12-08 12: 18: 38,960 26825-26825 / br.com.kappauni.teste I / Clicked: Testeaaa Testeaaa

    
asked by anonymous 08.12.2014 / 15:28

1 answer

3

There are several ways to get the values of the clicked row of a listView , one of them is through view that is passed in onItemClick :

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int posicao, long id) {

        //Substitua R.id.TextViewNome pelo id da textView que recebe o campo "nome"
        TextView tvNome = (TextView)view.findViewById(R.id.TextViewNome);
        Log.i("Clicado", posi + "\t" + tvNome.getText.toString());
    }
 }

Another is to read directly from the database using id that is passed in onItemClick :

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int posicao, long id) {

        Cursor c = bancosqlite.rawQuery(
              "SELECT nome,sobrenome,nascimento
               FROM tabelateste 
               WHERE id = ?",new String[] {Long.toString(id)});

        //Se o seu adapter for do tipo cursorAdapter a linha anterior pode ser
        //substituida por:
        //Cursor c = (Cursor) parent.getItemAtPosition(position);

        String testeNome = c.getString(c.getColumnIndex("nome")).trim();
        String testeSobrenome = c.getString(c.getColumnIndex("sobrenome")).trim();
        String testeNascimento = c.getString(c.getColumnIndex("nascimento")).trim();

        Log.i("Clicado", posi + "\t" + testeNome);
    }
 }
    
08.12.2014 / 16:29