Created
June 14, 2018 02:32
-
-
Save oriolrivera/4daec6f502bf691bd24b9c0610212361 to your computer and use it in GitHub Desktop.
Add hashmap key value to spinner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Map<String, String> spinnerValueMap = new HashMap<String, String>(); | |
List<StringWithTag> itemList = new ArrayList<StringWithTag>(); | |
String loadsearchkeyID; | |
spinnerValueMap.put(“Key”,”Value”); | |
for (Entry<String, String> entry : spinnerValueMap.entrySet()) { | |
String key = entry.getKey(); | |
String value = entry.getValue(); | |
/* Build the StringWithTag List using these keys and values. */ | |
itemList.add(new StringWithTag(value, key)); | |
} | |
ArrayAdapter<StringWithTag> spinnerAdapter = new ArrayAdapter<StringWithTag>(getActivity(), android.R.layout.simple_spinner_item, itemList); | |
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); | |
spinner.setAdapter(spinnerAdapter); | |
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { | |
@Override | |
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { | |
StringWithTag swt = (StringWithTag) parent.getItemAtPosition(position); | |
loadsearchkeyID = (String) swt.tag; | |
} | |
@Override | |
public void onNothingSelected(AdapterView<?> parent) { | |
// sometimes you need nothing here | |
} | |
private static class StringWithTag { | |
public String string; | |
public Object tag; | |
public StringWithTag(String string, Object tag) { | |
this.string = string; | |
this.tag = tag; | |
} | |
@Override | |
public String toString() { | |
return string; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment