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
  • 910,778
  • 176
  • 2,215
  • 2,253
Brejuro
  • 2,851
  • 6
  • 27
  • 48
  • 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
  • 19,651
  • 5
  • 34
  • 54
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,066
  • 8
  • 22
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
  • 1,924
  • 1
  • 20
  • 21