有没有办法按键从hashmap中随机选择值?

时间:2015-04-13 21:13:34

标签: java android random hashmap

假设我有两个ImageButtons,一个HashMap,以及drawable(资源)中的图片,这些图片作为值放在此HashMap中。 我需要使用Randomizer从HashMap按键中随机选择一个值,然后将此值(即图片)指定给ImageButton作为其背景。 例如,如果单击第一个按钮,则随机更改第二个按钮的背景。

所以这就是我已经拥有的:

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);
        }
    });
  }
}

2 个答案:

答案 0 :(得分:3)

您可以通过将Map的所有键放入List(或数组)中随机选择一个键,然后从该List(或数组)中随机选择一个索引

List<Integer> keys = new ArrayList<Integer>(someHashMap.keys());
Integer randomKey = keys.get(random.nextInt(keys.size()));
Object randomValue = someHashMap.get(randomKey);

答案 1 :(得分:0)

您可以完全取消Map,只需使用一组int值。

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)]);