Collect data from an ArrayList to display them in a Spinner

-1

My question is this: I'm doing a CRUD on Android and I have a problem on the UPDATE. As you can see, I want to collect all the data in a Bundle and then print them with setText () in the update form. The problem is that the last data, the displacement (which is commented because if it does not give me an error), I want to show it in a Spinner and I do not know what the method is.

This is how I send the data when I click on the items in the list:

listView_motocicletas.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int posicion, long id) {
            Toast.makeText(Listado_motocicletas.this, listado_motocicletas.get(posicion), Toast.LENGTH_SHORT).show();
            int clave= Integer.parseInt((listado_motocicletas.get(posicion).split(" ")[0]));
            String marca= listado_motocicletas.get(posicion).split(" ")[1];
            String matricula= listado_motocicletas.get(posicion).split(" ")[2];
            String cilindrada= listado_motocicletas.get(posicion).split(" ")[3];
            Intent intent = new Intent(Listado_motocicletas.this, Actualizar_moto.class);
            intent.putExtra("id", clave);
            intent.putExtra("marca", marca);
            intent.putExtra("matricula", matricula);
            intent.putExtra("cilindrada", cilindrada);
            startActivity(intent);
        }
    });

}

And here is where I pick it up:

Bundle b= getIntent().getExtras(); 
    if (b!=null){ 
        id=b.getInt("id");
        marca=b.getString("marca");
        matricula=b.getString("matricula");
        cilindrada=b.getString("cilindrada");
    }

    marca_actualizar=(EditText) findViewById(R.id.marca_actualizar);
    matricula_actualizar=(EditText) findViewById(R.id.matricula_actualizar);
    cilindrada_actualizar=(Spinner) findViewById(R.id.cilindrada_actualizar);
    boton_actualizar=(Button)findViewById(R.id.boton_actualizar);
    boton_eliminar=(Button) findViewById(R.id.boton_eliminar);

    marca_actualizar.setText(marca);
    matricula_actualizar.setText(matricula);
    //cilindrada_actualizar.getSelectedItem();

The displacement I pick it up but I can not establish it because if not, I get an error.

    
asked by Jorge García Cano 01.01.2019 в 13:16
source

1 answer

0

Save the selection ( posicion ) of the Spinner as int , not the string that contains the item:

 int itemSeleccionado = cilindrada_actualizar.getSelectedItemPosition();

Per intent is sent as int :

 int cilindrada= listado_motocicletas.get(posicion).split(" ")[3];
 ...
 intent.putExtra("cilindrada", cilindrada);

And you get the same as int , to show it in the spinner with setSelection , it's not with get :

 int cilindrada=0;
 cilindrada=b.getInt("cilindrada");
 cilindrada_actualizar.setSelection(cilindrada);

To access your resource array:

 String[] tu_array = getResources().getStringArray(R.array.tu_spinner_array);

If you only want to show the text:

 tu_TextView.setText(tu_array[cilindrada]);
    
answered by 02.01.2019 в 03:15