Spinner And Onitemselected
Solution 1:
I do not know how to get the item with tag (courseid) in this array after the user click the item in spinner.
If you only want to get the courseid
from each row, you do not need a custom OnItemSelectedListener. Simple use:
// Let's use the regular listener vvvvvvvvvvvvvvvvvvvvvv
spinner.setOnItemSelectedListener(newOnItemSelectedListener() {
@OverridepublicvoidonItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
HashMap<String, String> map = arrList.get(position);
String id = map.get("courseid");
// Do something
}
@OverridepublicvoidonNothingSelected(AdapterView<?> adapter) {}
});
Where arrList
is the class variable that holds the data for your SimpleAdapter.
i do like this but kothing happend and logcat not having error
That is because you just created arrList
, so it is empty:
ArrayList<HashMap<String, String>> arrList = new ArrayList<HashMap<String,String>>();
for (HashMap<String, String> map2 : arrList) { // This arrList has no data!
You need to use the ArrayList that you used when you created your SimpleAdapter.
newSimpleAdapter(this, arrList, ...);
This is the ArrayList you must use and it must be a class variable.
Solution 2:
Your post is a little hard to understand, but i think i have an idea. you have a single spinner populated by an arraylist with multiple hashmaps at each index for each "rung" on the spinner. So when the item is selected, you want to get the single key that is selected and do something with it. Well as we know, a hashMap
isn't really indexed so we have to use alternative methods to get at it, correct?
ArrayList<HashMap<String, String>> arrList = new ArrayList<HashMap<String,String>>();
spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener() {
@Override
publicvoid onItemSelected(AdapterView<?> adapterView, View view,
int position, long id) {
// for each key in the hashMap at this position..for (String key : arrList.get(position).keySet()) {
if (key == "Calculus") {
// do something
} elseif (key == "Physics") {
// do something else
}
}
}
The idea is really simple here. you get the entire keySet of the hashMap at the index of the position selected on the spinner and you do something for each key in it, depending on what it says. This should work without too much hassle, assuming you only have one key-value pair there.
BUT I have to say that you should really re-think the design a bit. You have multiple hashMap containers just holding one thing each. It really is a waste of resources. it's hard to recommend an alternative because i don't exactly know how this data is being used, but you should know that making objects in java isn't free.
Post a Comment for "Spinner And Onitemselected"