I am trying to create a spinner whose values ββI am populating from an xml resource using an ArrayAdapter.
I also want to give the resource elements some "id" or "value". How can I get these values ββinside the onItemSelected () callback?
Here is the Java code.
package com.waus.waus; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.Toast; public class Register extends Activity implements OnItemSelectedListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.register); Spinner countrySpinner = (Spinner) findViewById(R.id.country_code_spinner); ArrayAdapter<CharSequence> countryAdapter = ArrayAdapter.createFromResource(this, R.array.country_codes, android.R.layout.simple_spinner_item); countryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); countrySpinner.setOnItemSelectedListener(this); countrySpinner.setAdapter(countryAdapter); } @Override public boolean onCreateOptionsMenu(Menu menu) {
This is the XML file I want to use.
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="country_codes"> <item value="91">India</item> <item value="1">United States</item> // OR <item id="1">United States</item> </string-array> </resources>
How to do this without using 2 resource files. that is, for codes, and the other for names.
source share