2012-03-29 1 views
2

私のドローブフォルダからイメージビュー(onClick of imageview)にどのイメージが設定されているのかを調べる必要があります。そのためには、私のdrawableフォルダからimageviewの背景と画像のdrawableを比較する必要があります。私はそれのためのいくつかの方法を見つけましたが、私の場合はそれのどれも動作しません。 "mBackgroundResource"という画像ビューの1つのプロパティが、画像の同じ整数値をドロウアブルフォルダに保持していることがわかりました。どのように私のdrawableからimageviewに設定されている画像を見つけることができますか?

int i = R.drawable.skype; (2130837526) 

ImageViewののmBackgroundResource= 2130837526

のでmBackgroundResource値を取得する方法はありますか?だから私はそれを比較しようとすることができます。誰でもこれを解決するのに役立つことができますか?

ここに私のコードです。

Context mContext; 
Drawable skype; 

skype = mContext.getResources().getDrawable(R.drawable.skype); // in the constructor of the adapter. 

public void onClick(View v) 
    { 
     ImageView img = (ImageView)v.findViewById(R.id.img_call_icon); 
     Drawable img_id = img.getBackground(); 
    } 

今、私が試した

が...次

私はそうアダプタで午前、(どこで行方不明です?)

/***********************************************************************************/ 

    if(img_id == skype) 
    { 
     // Not working... 
    } 

/***********************************************************************************/ 
Bitmap bitmap = ((BitmapDrawable)img_id).getBitmap(); 
Bitmap bitmap1 = ((BitmapDrawable)skype).getBitmap(); 

if(bitmap == bitmap1) 
{ 
    // Not working... 
} 
/***********************************************************************************/ 
Object tag = img.getTag(); 
int i = R.drawable.skype; 

if(tag != null && ((Integer)tag).intValue() == i) 
{ 
    // Not working... 
} 
/***********************************************************************************/ 
int j = ((Integer)tag).intValue() ; // always return 0.. 
int i = R.drawable.skype; 

if(i == j) 
{ 
    // Not working... 
} 
/***********************************************************************************/ 
Boolean b = img_id.equals(skype); // return FALSE, Not working... 
/***********************************************************************************/ 
ImageView imageView = (ImageView)v.findViewById(R.id.img_call_icon); 
assert(i == imageView.getId()); 
Integer integer = (Integer) imageView.getTag(); // always fill with null... 
integer = integer == null ? 0 : integer; 
    switch(integer) 
    { 
    case R.drawable.skype: 
     Toast.makeText(mContext, "skype", Toast.LENGTH_SHORT).show(); 
     break; 

    case R.drawable.call: 
     Toast.makeText(mContext, "call", Toast.LENGTH_SHORT).show(); 
     break; 
    } // Not working... 

答えて

7

あなたは、アダプタでImageViewのの描画可能に設定していますか?

もしそうなら、あなただけそうのように、ビューのタグで描画可能なIDを救うことができる:

ImageView imageView = (ImageView) v.findViewById(R.id.img_call_icon); 
imageView.setTag(R.drawable.a_drawable); 
imageView.setDrawableResource(R.drawable.a_drawable); 

とImageViewのである描画可能なチェックする:

ImageView imageView = (ImageView) v.findViewById(R.id.img_call_icon); 
Object tag = imageView.getTag(); 
int id = tag == null ? -1 : (int) tag; 
switch(id) 
{ 
case R.drawable.skype: 
    Toast.makeText(mContext, "skype", Toast.LENGTH_SHORT).show(); 
    break; 

case R.drawable.call: 
    Toast.makeText(mContext, "call", Toast.LENGTH_SHORT).show(); 
    break; 
} 
+0

完璧に働いて答えを上のスポット、ありがとう、その作品.. –

関連する問題