4
I have a spinner that lists the types of person (physical and legal - respectively Ids = 1 and 2) and would like when selecting one of them, to be printed in a Toast your ID.
//array tipoPessoa
private String[] tipoPessoa = new String[]{"física", "jurídica"};
//array idPessoa
private String[] tipoPessoaId = new String[]{"1", "2"};
The following code snippet is how I can print the id of the item, however I want to print its value on array tipoPessoaId
in accordance with his position in tipoPessoa
:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, tipoPessoa);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp = (Spinner) findViewById(R.id.sp_tipo_pessoa);
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String idTipoPessoa = "O ID é: " + parent.getItemIdAtPosition((int) id);
Toast.makeText(parent.getContext(), idTipoPessoa, Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Wants you to go
fisica
prints1
because it is the element in the same position0
of the other array, andjurídica
prints2
for the same reason ?– Isac
That’s right Isac
– Thyago Dias