1

Hi guys i got a situation with a imageview, I set dynamically the drawable source and the text, something like this, too similar to this question

title.setText(items.get(position).getTitle());
icon.setImageResource(this.icons.getIcon(items.get(position).getIcon()));

the xml with the onClickIcon on the imageview , what i need its to know which images id drawable come and check if its equal to mine

<ImageView android:id="@+id/image" 
        android:onClick="onClickIcon"
         />

Activity

public void onClickIcon(View v) {

    int id =  v.getId();//instead i need the int of the drawable image

    switch (id) {
    case R.drawable.info:
        //do something
        break;
    case R.drawable.links_icon:
        //do something
        break;
    case R.drawable.map_icon:
        //do something
        break;
    default:
        break;
    }
}

Thanks guys in advance , im also sorry for my english, ill try to improve it next time.

Cheers

Community
  • 1
  • 1
ron
  • 440
  • 9
  • 21

2 Answers2

3

You can use setTag() method..

ImageViewsetTag(R.id.ImageView,this.icons.getIcon(items.get(position).getIcon()));

and use getTag(R.id.ImageView) in onClick method..

Niranj Patel
  • 35,286
  • 11
  • 96
  • 128
  • it didnt work , i set the icon.setTag(this.icons.getIcon(items.get(position).getIcon())), and i get null in id, :( thanks anyways m8 – ron Apr 09 '11 at 13:16
  • 1
    sorry m8 works perfect, i miss one thing , thank you very much – ron Apr 09 '11 at 13:27
1

Here is another workaround that is similar to CapDroid's answer:

You can create your own getImageResource() or getDrawableId() function by using the ImageView tags.

In onCreate():

imageView0 = (ImageView) findViewById(R.id.imageView0);
imageView1 = (ImageView) findViewById(R.id.imageView1);
imageView2 = (ImageView) findViewById(R.id.imageView2);

imageView0.setTag(R.drawable.apple);
imageView1.setTag(R.drawable.banana);
imageView2.setTag(R.drawable.cereal);

Then, if you like, the simple function to get the drawable id:

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

I hope this helps you, it sure made my work easier.

Anonsage
  • 7,194
  • 3
  • 41
  • 50