How to take the value of a Spinner and continue the form

Asked

Viewed 5,258 times

1

Good afternoon, I have a simple form in my android app, where the user writes name and register an alert, but in the alert frequency field I inserted a spinner(selectbox android) with options of numbers that are strings.

txtFrequencia.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) this);

    List<String> frequencia = new ArrayList<String>();
    frequencia.add("2");
    frequencia.add("4");
    frequencia.add("6");
    frequencia.add("8");
    frequencia.add("10");
    frequencia.add("12");
    frequencia.add("24");


    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, frequencia);

    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    txtFrequencia.setAdapter(dataAdapter);

This is the part where I declare the spinner, but the problem is happening in my class that registers in the database, not sure what to insert in it, I’m using getOnItemSelectedListener() to try to get the value but it’s not working, follow the code:

 private long loadType() {
    Type type = new Type();
    millisIdentificador = Calendar.getInstance().getTimeInMillis();
    type.setContent("Alerta;" + String.valueOf(millisIdentificador) + ";" + txtNomeAlerta.getText().toString()
            + ";" + (btnTodoDia.isChecked() ? 1 : 0) + ";" + (btnFimdeSemana.isChecked() ? 1 : 0)
            + ";" + Integer.parseInt(txtFrequencia.getOnItemSelectedListener().toString()) + ";"
            + Integer.parseInt(txtQuantComprimidos.getText().toString()) + ";"
            + Integer.parseInt(txtQuantDias.getText().toString()));
    type.setComentarCard(0);
    type.setCompartilharCard(0);
    type.setCurtirCard(0);
    return dataStoreType.addType(type);
}

Does anyone have any idea how I can put the correct value of the spinner position.

1 answer

4

Hello, instead of getOnItemSelectedListener() use getSelectedItem() which returns an object, or getSelectedItemPosition() which returns the position of the list of the item that was chosen by the user.

Example:

int posicao = txtFrequencia.getSelectedItemPosition();
String itemSelecionado = frequencia.get(posicao);

I hope it helped!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.