开发者

Check drawable id from imageView

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" 
        开发者_JS百科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


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..


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜