1

I have a map of <CheckBox, ImageButton>.

I want to be able to iterate over this map and change the image of each ImageButton. Is there any way I can do this? getValue() doesn't seem to let me use the methods associated with each ImageButton.

CommonsWare
  • 954,112
  • 185
  • 2,315
  • 2,367
Brejuro
  • 3,181
  • 6
  • 31
  • 55
  • Isn't this just iterating using `getValue()`? I'm unable to call `setImageBitmap()` on `getValue()`. It seems like I'd have to create another ImageButton object and set that equal to `getValue()`. But then if I change that new ImageButton, wont my original one stay the same? – Brejuro Aug 16 '15 at 21:33
  • See my updated answer – aProperFox Aug 16 '15 at 21:35

4 Answers4

1
// support you define your hash map like this
HashMap<CheckBox,ImageButton> hs = new HashMap<CheckBox,ImageButton>();
// then
for(Map.Entry<CheckBox, ImageButton> e : hs.entrySet())
{
    ImageButton imgBtn = e.getValue();
    // do whatever you like to imgBtn
}
scoot
  • 11
  • 1
0

A HashMap? Use HashMap.values() to get a Collection of the saved Values. http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html#values()

Luka Jacobowitz
  • 21,337
  • 5
  • 37
  • 57
0

You can iterate over the keys of a Map using keySet, and get the value associated with each individual key:

for (CheckBox cb : checkBoxes.keySet()) {
    ImageButton btn = checkBoxes.get(cb);
}
Sebastian
  • 1,070
  • 8
  • 23
0

Taken from this answer

You have to cast the result of getValue to an ImageButton to use its functions.

Iterator it = mp.entrySet().iterator();
while (it.hasNext()) {
    Map.Entry pair = (Map.Entry)it.next();
    ((ImageButton)pair.getValue()).setImageBitmap(bitmap);
}
Community
  • 1
  • 1
aProperFox
  • 2,044
  • 1
  • 21
  • 21