Load Spinner from sqlite Android database

1

Dear, I am uploading a Spinner with information obtained from a table of database SQLite of android, I have no problems in loading the Spinner , but if at the time of obtaining the selected data and store it in another database table. This is the way I use to load the Spinner . I would appreciate your guidance since I am new to Android. Thank you very much.

BaseDatos bd  = new BaseDatos(getApplicationContext());

        final Cursor c = bd.getReadableDatabase().rawQuery("SELECT id AS _id,nombre FROM Valores",null);

        final SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,android.R.layout.simple_spinner_item,c,new String[]{"nombre"},new int[] {android.R.id.text1});

        adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);

        spValores.setAdapter(null);
        spValores.setAdapter(adapter);
    
asked by Cristian Bossardt 25.08.2017 в 15:50
source

1 answer

1

You need the following method:

TuSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
                        String ValorSeleccionado=TuSpinner.getSelectedItem().toString(); //Obtiene el valor del Spinner
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> adapterView) {

                    }
                });

This method allows you to perform an action each time the selected item is changed, as you can notice, in the example I keep in a String the value that the Spinner has. Also, the same method contains a Listener for when nothing is selected in the Spinner, in case you need it.

EDIT Another way you can work is to save the results of your query in an Arraylist, at least the data that you need to show in the Spinner, in such a way, you could recover it in the following way: (the proposed code goes inside OnItemSelected )

String Valor = (String) TuArrayList.get(position);
    
answered by 25.08.2017 / 16:10
source