I'm still learning Android, I need to have a spinner and for it to display a list of string values. However, when I submit that data later on I need a corresponding integer value.
Similarly to how a HTML Select uses <option value="2">BlahBlah</option>
.
From searching I've come up with a method which works, just wondering if anyone had any suggestions for improving the code?
I've got the two variables setup:
private String[] arrMuppetNames = {"Kermit","Gonzo","Fuzzy","Animal"};
HashMap<String, Integer> hashMuppets = new HashMap<String, Integer>();
hashMuppets
is built up with a simple set of "Kermit":1,"Gonzo":2 etc.
Then to stop the code running on load I've added a "--please select--"
to index 0.
//Add "please select" to spinner
arrNewArray[0] = this.getString(R.string.muppet_select);
for(int i=0; i < arrMuppets.length; i++){
arrNewArray[i+1] = arrMuppets[i];
}
Obviously I add this array arrNewArray
to the spinner using ArrayAdapter
. Then use the OnItemSelectedListener
.
private Spinner.OnItemSelectedListener spinnerListener = new Spinner.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
Spinner spinner = (Spinner) findViewById(R.id.muppet_spinner);
//Arg 3 is selected index. Lets ensure we didn't select our "Please select"
if(arg3 != 0){
//Get Selected Item and convert to corresponding value using hashMap
String strSelectedMuppet = spinner.getSelectedItem().toString();
int intCatID = hashMuppets.get(strSelectedMuppet);
//Show toast of value
Toast.makeText(getApplicationContext(), String.valueOf(intCatID), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
//Do nothing
}
};
If anyone has any suggestions or pointers I'd very much appreciate it. I'm happy to post the full code if you'd like, but I'm hoping the above short example is pretty self explanatory.
Thanks
1 Answer 1
As long as you are hard-coding the values, I would go with an enum
public enum Muppets {
Kermit, Gonzo, Fozzie, Animal, Swedish_Chef, Miss_Piggy,
Statler, Waldorf, Beaker, Beauregard, Dr_Benson_Honeydew,
Crazy_Harry, Rowlf, Dr_Teeth, Zoot, Janice, Floyd;
// Sorry, I just thought your list of muppets was way too short
public int getNumber() {
return this.ordinal() + 1;
}
@Override
public String toString() {
return super.toString().replaceAll("_", " ");
}
}
(To make things easier for you, you can also add your Please Select
as a muppet and override the toString
method to return a custom string if this == PLEASE_SELECT
, this would also remove the need for adding one in getNumber
, and would greatly simplify adding the items to the adapter)
Then I would use the ArrayAdapter
as ArrayAdapter<Muppet>
, so that you can get the selected item like this:
Muppet selectedMuppet = spinner.getSelectedItem();
int value = selectedMuppet.getNumber();
To get an array of all the muppet enums, use Muppets.values()
.
If you want the possibility to add muppets dynamically at run-time, I would make a List<String>
and get the position of an item using list.indexOf(muppetName)
.