SQLITE cursor picking up last value

Asked

Viewed 759 times

0

How to make the SQLITE Cursor, make the variables not stop at the last value ? I made a Setadapter to which he lists all the Listview data, so far so good, but when I put some variable that is inside WHILE, it prints 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 name is the "testenome" which always shows me the last value, I do not know why .

NOTE: When clicking on the Listview item containing the name "Testeaaa", it opens another Activity without problems, but others do nothing .

Message on the logcat

12-08 12:18:24.680 26825-26825/br.com.kappauni.test I/Clicado Diego Kappaun Testeaaa 12-08 12:18:29.960 26825-26825/br.com.kappauni.test I/Clicked Diella Heckler granella Testeaaa 12-08 12:18:38.960 26825-26825/br.com.kappauni.test I/Clicked Testeaaa Testeaaa

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • 1

    The while traverse the entire cursor, from the first record to the last, assigning the values of the fields always to the same variables. So the variables will have the value of the last record. To help we need to know what the purpose of your code is.

  • I need that when you click on any of the names, it recovers the value of the bank that was clicked on. When I click on my name and my girlfriend’s name it doesn’t work, only when I click on "Testeaaa" it opens the screen normally, I just need to make it work to continue the project. I edited the post and put 2 images for viewing and sorry for the bad editing.

1 answer

3


There are several ways to get the values of a clicked line listView, one of them is through the view which is spent 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 bank using the id which is spent 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);
    }
 }
  • You could give an example using listview ?

  • ??? The example I gave is using listview.

  • I was referring there instead of "Textview tvName = (Textview)view.findViewById(R.id.Textviewnome);". Thanks for the reply, as soon as I get home I will try to implement this second option and I will give a position .

  • The first method starts from the principle that you have a view your for the items of listview. If you’re using the view android.R.layout.simple_list_item_1 use (TextView)view.findViewById(android.R.id.text1); or use the second method.

  • With his reply, I opened my mind a little more and used comparisons to see if a given item was clicked to edit the separate fields. I will give as settled . Thank you .

Browser other questions tagged

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