user6942836
user6942836

Reputation:

how to access drawable pairs in hashmap

I try access to drawable resource that I put in hashmap object this is my relevant code:

  private void pairImagesCollection() {
    mImages.put(R.drawable.img1, R.drawable.img1shadow);
    mImages.put(R.drawable.img2, R.drawable.img2shadow);
    mImages.put(R.drawable.img3, R.drawable.img3shadow);
    mImages.put(R.drawable.img4, R.drawable.img4shadow);
}

 private void checkMatch(View dragView, View view) {
        ImageView target = (ImageView) view;
        ImageView dragged = (ImageView) dragView;
        Drawable image = dragged.getDrawable();
        int imgId = mImages.get(Integer.valueOf(image)); // wrong, I don't know how to do it ?!
        target.setImageResource(imgId);
    }

Any help will be appraised!

Upvotes: 0

Views: 539

Answers (3)

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

You can use Iterator

Iterator itObj = hashMapOBJ.entrySet().iterator();
                        while (itObj .hasNext()) {
                            Map.Entry pair = (Map.Entry) itObj.next();

                                String Key = (String) pair.getKey();
                                Drawable Value = (Drawable) pair.getValue();
                                hashMapOBJ.put(Key, Value);
                                System.out.println("Key: " + Key + "------>" + Value);

                       }

Upvotes: 1

RoShan Shan
RoShan Shan

Reputation: 2954

int imgId = mImages.get(Integer.valueOf(image));

You cannot get Resource/Drawable Id from ImageView through Drawable. With codes above, You can set tag for ImageView dragged. After that get value with with your mImages.

Before you checkMatch, you set tag for dragView as Drawble, something like that:

dragged.setTag(R.drawable.img1);

And now you can getDrawableId and

imgId = getDrawableId(dragged);

    private int getDrawableId(ImageView iv) {
        return (Integer) iv.getTag();
    }

Upvotes: 0

gvlachakis
gvlachakis

Reputation: 474

You got a hashmap that stores references to an image and its shadow right? Which image you want to put on the image view? You have to iterate through your map and put the image you want:

Iterator iterator = map.entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry pair = (Map.Entry)iterator.next();
        System.out.println(pair.getKey() + " = " + pair.getValue());
         }

If you want to put the value image then you put the pair.getKey() else you put the pair.getValue().

target.setImageResource(R.drawable.xxx);

Upvotes: 0

Related Questions