Set Value For Spinner With Custom Adapter In Android
I am developing a android application with spinner in a form. The spinner items and spinner values are different. I want to collect all the value from the from including spinner an
Solution 1:
@Haresh Chhelana example is good, However if you want to show both name and code in spinner after selecting, check this out.
List<Map<String, String>> items = newArrayList<Map<String, String>>();
for (int i = 0; i < JA.length(); i++) {
json = JA.getJSONObject(i);
mapData = newHashMap<String, String>();
mapData.put("name", json.getString("Name"));
mapData.put("code", json.getString("Code"));
items.add(mapData);
}
SimpleAdapter adapter = newSimpleAdapter(this, items, android.R.layout.simple_list_item_2, newString[] {
"name", "code" }, new int[] { android.R.id.text1, android.R.id.text2 });
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
And Spinner selected item callback
spinner.setOnItemSelectedListener(newOnItemSelectedListener() {
@OverridepublicvoidonItemSelected(AdapterView<?> parent, View view, int position, long id) {
Map<String, String> selectedItem = (Map<String, String>) parent.getSelectedItem();
String name=selectedItem.get("name");
String code=selectedItem.get("code");
}
@OverridepublicvoidonNothingSelected(AdapterView<?> parent) {
}
});
Post a Comment for "Set Value For Spinner With Custom Adapter In Android"