Put the item that I want from a list in the Android spinner

0

What I am trying to do is for a user to select an element through a spinner, I save that element through the position and what I want is that when the user returns to edit the spinner he will be shown the one he marked first.

I leave here part of the code, statements:

private Spinner spinnerSeason;

private int season = 0;

private String[] Seasons = new String[] {
            "Estacion",
            "Indiferente",
            "Invierno",
            "Primavera",
            "Verano",
            "Otoño"
    };

This would fit into the onCreate of the activity:

ArrayAdapter<String> spinnerArrayAdapterSeason  = new ArrayAdapter<String>(
                this,android.R.layout.simple_spinner_item,Seasons);
        // Specify the layout to use when the list of choices appears
        spinnerArrayAdapterSeason.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);        // Apply the adapter to the spinner
        spinnerSeason.setAdapter(spinnerArrayAdapterSeason);
        spinnerSeason.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (parent.getId()) {
                    case R.id.spinnerSeason:
                        //tv.setText(tv.getText() + parent.getItemAtPosition(position).toString());
                        season = position;
                        break;
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                //Another interface callback
            }
        });

Initialize the spinner:

    spinnerSeason = (Spinner) findViewById(R.id.spinnerSeason);

This is where I should load the spinner element that the user previously set. From a method, I tell it to run if the user is editing, but does not load the one that the user frames but the first one of the vector.

    spinnerSeason.setVerticalScrollbarPosition(season);

season has the value that the user previously set. fails the method of the Spinner class.

    
asked by CMorillo 31.03.2018 в 18:29
source

1 answer

1

use array SharedPreferences :

   spinnerSeason.setAdapter(spinnerArrayAdapterSeason);
    //para obtener la posicion del spinner desde SharedPreferences:
    final SharedPreferences[] sharedPref = {getActivity().getSharedPreferences("SpinnerEj", MODE_PRIVATE)};
    int spinnerValor = sharedPref[0].getInt("itemSeleccionado",-1);
    if(spinnerValor != -1) {
        // configuras el valor del spinner guardado
        spinnerSeason.setSelection(spinnerValor);
    }

    spinnerSeason.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            // aquí obtienes el valor del spinner (int position)y lo guardas dentro de SharedPreferences:
            int valorSeleccionado = spinnerSeason.getSelectedItemPosition();
            sharedPref[0] = getActivity().getSharedPreferences("SpinnerEj",0);
            SharedPreferences.Editor prefEditor = sharedPref[0].edit();
            prefEditor.putInt("itemSeleccionado",valorSeleccionado);
            prefEditor.commit();

        }

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

        }
    });

Another option would be to use onSaveInstanceState , but in this case if the app is closed the values are not saved.

Example if you want to save it in a database, in this case SQLite with Content Provider, to see it in a ListView / RecyclerView.

And assuming that the array is in resources such as: R.array.seasons .

To save the position of the spinner item, in your SQLite the column must be of type INTEGER

TuTabla.TU_COLUMNA + " INTEGER, " + ...

You get a int of the position to save:

int itemSeleccionado = spinnerSeason.getSelectedItemPosition();

You save it in your database like this:

contentValues.put(TuTabla.TU_COLUMNA, itemSeleccionado);

In the ViewHolder you have to add the ArrayAdapter:

ArrayAdapter<CharSequence> spinnerArrayAdapterSeason = ArrayAdapter.createFromResource(v.getContext(), R.array.seasons, android.R.layout.simple_spinner_item);
spinnerSeason.setAdapter(spinnerArrayAdapterSeason);

In your adapter you have to refer to the array:

int[] numSeleccionado = holder.itemView.getResources().getIntArray(R.array.seasons);

And so you add it to your view:

holder.spinnerSeason.setSelection(numSeleccionado);
    
answered by 31.03.2018 в 19:43