1

Suppose that I have two ImageButtons, a HashMap, and pictures in drawable(resources) that are placed in this HashMap as a value. I need to randomly choose a value from the HashMap by key using Randomizer and then assign this value (that is a picture) to the ImageButton as it's background. For example, if the first button is clicked, the second button's background will be randomly changed.

So this is what I already have:

public class MainActivity extends Activity {

  final Resources res = getResources();
  final Random random = new Random();

  final ImageButton imgButt1 = (ImageButton) findViewById(R.id.imageButton1);
  final ImageButton imgButt2 = (ImageButton) findViewById(R.id.imageButton2);

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Drawable coub1 = res.getDrawable(R.drawable.coub1);
    Drawable coub2 = res.getDrawable(R.drawable.coub2);
    Drawable coub3 = res.getDrawable(R.drawable.coub3);
    Drawable coub4 = res.getDrawable(R.drawable.coub4);
    Drawable coub5 = res.getDrawable(R.drawable.coub5);
    Drawable coub6 = res.getDrawable(R.drawable.coub6);
    Drawable coub7 = res.getDrawable(R.drawable.coub7);
    Drawable coub8 = res.getDrawable(R.drawable.coub8);
    Drawable coub9 = res.getDrawable(R.drawable.coub9);
    Drawable coub10 = res.getDrawable(R.drawable.coub10);

    final Map<Integer,Object> someHashMap = new HashMap<Integer,Object>();
    someHashMap.put(1, coub1);
    someHashMap.put(2, coub2);
    someHashMap.put(3, coub3);
    someHashMap.put(4, coub4);
    someHashMap.put(5, coub5);
    someHashMap.put(6, coub6);
    someHashMap.put(7, coub7);
    someHashMap.put(8, coub8);
    someHashMap.put(9, coub9);
    someHashMap.put(10, coub1);

    imgButt1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Object[] values = someHashMap.values().toArray();
            Object randomValue = values[random.nextInt(values.length)];
            int drawableID = values.;
            imgButt2.setBackgroundResource(drawableID);
        }
    });

    imgButt2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Object[] values = someHashMap.values().toArray();
            Object randomValue = values[random.nextInt(values.length)];
            int drawableID = myImages.getResourceId(randomInt, -1);
            imgButt1.setBackgroundResource(drawableID);
        }
    });
  }
}

Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
Vibilia
  • 11
  • 3

2 Answers2

3

You can randomly choose a key by placing all the keys of a Map into a List (or array), then randomly choosing an index from that List (or array)

List<Integer> keys = new ArrayList<Integer>(someHashMap.keys());
Integer randomKey = keys.get(random.nextInt(keys.size()));
Object randomValue = someHashMap.get(randomKey);
copeg
  • 8,290
  • 19
  • 28
0

You could dispense with the Map completely and just use an array of int values.

int[] ids = {R.drawable.coub1, R.drawable.coub2, R.drawable.coub3, R.drawable.coub4, 
             R.drawable.coub5, R.drawable.coub6, R.drawable.coub7, R.drawable.coub8, 
             R.drawable.coub9, R.drawable.coub10};
// Then, when you need a random Drawable 
Drawable drawable = res.getDrawable(ids[random.nextInt(ids.length)]);
Paul Boddington
  • 37,127
  • 10
  • 65
  • 116