How to fill a listview with data from a sqlite database

0

Good afternoon it turns out that I have a button to enter data to the bd and it works perfectly, a text field called code with a listview called list1, the idea is to bring the data according to the code and fill the list by clicking on a button but I have no idea how to do it

    
asked by juan perez 10.09.2018 в 19:01
source

1 answer

0

I think what you need is something like this:

 public void cargalist(){
        ArrayList <String> items = new ArrayList<>();
        SQLiteDatabase db=sql.getReadableDatabase();
        Cursor fila= db.rawQuery("select * from items",null);
        if(fila.moveToFirst()){
             do{
                items.add("Clave: "+fila.getString(0)+"\n"+
                "Cantidad: "+fila.getString(1)+"\n"+"Nombre: "+fila.getString(2));
             }while (fila.moveToNext());
        }
        ArrayAdapter<String> adaptador=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,items);
        listait.setAdapter(adaptador);
    }

The only thing you do is the query is what you want, and put it into a arrayadapter as shown in the example

    
answered by 11.09.2018 / 00:40
source