Load Spinner with SQLite Android database

1

Hello, I need help to load a spinner from a database as easily as possible.

I have a class where doing all sorts of methods would ideally be that from the activity call those methods and load the spinner.

public ArrayList getAllClientes() {
    ArrayList list = new ArrayList<>();
    //String[] campos = new String[]{NOMBRE_CLIENTE, DIRECCION_CLIENTE};
    Cursor c = db.rawQuery("SELECT * FROM "+TABLA_CLIENTE, null);

    try {
        while (c.moveToNext()){
        Cliente cli = new Cliente();
        cli.setId(c.getInt(0));
        cli.setNombre(c.getString(1));
        cli.setDireccion(c.getString(2));
        list.add(cli);
        }
    } finally {
        c.close();
    }
    return list;
}

public Cursor getAllClientes(){
    return db.rawQuery("SELECT * FROM "+TABLA_CLIENTE, null);
}

Those are the methods I was trying to bring the data from the bd but I do not know if they serve me and that's how I do not know how to set the spinner

    
asked by callo33 27.10.2017 в 20:23
source

1 answer

1

First create your spinner

<Spinner
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/GameSpinner"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

Then use

genreSpinnerAdapter = new SimpleCursorAdapter(this,
    android.R.layout.simple_spinner_item,
    dataSource.getAllGenres(),
    new String[]{DataBaseScript.GenreColumns.NAME_GENRE},
    new int[]{android.R.id.text1},
    SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

I leave the reference link

    
answered by 27.10.2017 в 20:36