• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Populate spinner with Ids and Names

Ikr

Lurker
Is it possible to have something like web drop downs where we display strings in drop downs but we actually send IDs? something like <option value="500">Juice</option>

I have the following code to populate a spinner with an arraylist but I'm wondering if there is a way to display Male and Female but actually receives IDs like 1 and 2 when the form is posted.

Spinner spinnerGender = findViewById(R.id.spinnerGender);
String[] genderArray = new String[]{"Male","Female"};

List<String> genderList = new ArrayList<>(Arrays.asList(genderArray));

ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item,genderList);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerGender.setAdapter(adapter);
 
Back
Top Bottom