Listview returns only one Cursor item

Asked

Viewed 80 times

1

Good afternoon guys, I have a problem with my project. I have a Listview that receives information from Cursor and then sends it to the Adapter, but it is only pulling a record from my database, you could help me ?

public void lprod(SQLiteDatabase db){
    ArrayList<modelListprod> prod = new ArrayList<modelListprod>();

    listprod = new modelListprod();

    String descricao = "";
    String ean = "";
    String status = "";
    Double precoprod;
    String categoria;
    int cod;

    SQLiteDatabase d5 = dadosOpenHelper.getReadableDatabase();
    Cursor cursor5 = d5.query("produto" , new String[]{"descricao", "ean", "status", "precoprod", "codigocateg", "cod"},null, null, null   ,null,null,null);
   if (cursor5 != null) {
       if (cursor5.moveToFirst()) {

           do {


               descricao = cursor5.getString(0);
               ean = cursor5.getString(1);
               status = cursor5.getString(2);
               precoprod = cursor5.getDouble(3);
               categoria = cursor5.getString(4);
               cod = cursor5.getInt(5);
               listprod.setDescricao(descricao);
               listprod.setCategoria(categoria);
               listprod.setEan(ean);
               listprod.setPreco(precoprod);
               listprod.setStatus(status);
               listprod.getId(cod);


               //listadeprod.add(listprod);
           } while (cursor5.moveToNext());
       }
   }

    listadeprod.add(listprod);
    AdapterProd adapterProd = new AdapterProd(this, listadeprod);
    listproduto.setAdapter(adapterProd);

}
  • 1

    How many listprod are you creating? How often are you adding a listprod to the array?

  • listprod it sends the information to the adapter Model, the variable reading the listview is the listproduct, and the listprod it is only adding 1 time.

  • 1

    So it’s explained because the list only has one.

  • However the listprod it is placed only once, why it sends the database information to the modelListProd and through this model it talks with the layout of listr_products to fit the database data in its proper places. If I repeat or put some condition in it does not work.

1 answer

3


For each entry on the cursor a new modelListprod should be added to the array Prod.

View comments in code.

public void lprod(SQLiteDatabase db){
    ArrayList<modelListprod> prod = new ArrayList<modelListprod>();

    //Passa para dentro do while
    //listprod = new modelListprod();

    String descricao = "";
    String ean = "";
    String status = "";
    Double precoprod;
    String categoria;
    int cod;

    SQLiteDatabase d5 = dadosOpenHelper.getReadableDatabase();
    Cursor cursor5 = d5.query("produto" , new String[]{"descricao", "ean", "status", "precoprod", "codigocateg", "cod"},null, null, null   ,null,null,null);
   if (cursor5 != null) {
       if (cursor5.moveToFirst()) {

           do {

               //uma nova instância por cada registo
               listprod = new modelListprod();

               descricao = cursor5.getString(0);
               ean = cursor5.getString(1);
               status = cursor5.getString(2);
               precoprod = cursor5.getDouble(3);
               categoria = cursor5.getString(4);
               cod = cursor5.getInt(5);
               listprod.setDescricao(descricao);
               listprod.setCategoria(categoria);
               listprod.setEan(ean);
               listprod.setPreco(precoprod);
               listprod.setStatus(status);
               listprod.getId(cod);

               //Adiciona ao array
               listadeprod.add(listprod);

           } while (cursor5.moveToNext());
       }
   }
    //Passa para dentro do while
    //listadeprod.add(listprod);

    AdapterProd adapterProd = new AdapterProd(this, listadeprod);
    listproduto.setAdapter(adapterProd);
}

There would be more things to change, namely the name of some variables and restrict the method’s responsibility only to fill the array and return it.

  • Thank you so much, you helped me so much

  • 1

    I hope you realized where the problem was. Now it should make sense, for you, my first comment on the question.

  • Yes, did yes, I ended up getting confused and so I only passed once so he didn’t get into a loop. But thank you very much.

Browser other questions tagged

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