有条件地改变imageButton图像

时间:2012-03-20 21:40:20

标签: android listview imagebutton

我在listview中有一个imageButton,我想根据两种情况更改其图像。在第一种情况下,图像按钮被启用并具有图像。在第二种情况下,图像按钮被禁用,并且应该具有不同的图像。

public View getView(int position, View convertView, ViewGroup parent) {

    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.list_row, null);

    TextView title = (TextView)vi.findViewById(R.id.title); 
    TextView retail_price = (TextView)vi.findViewById(R.id.retail_price);
    TextView deal_price = (TextView)vi.findViewById(R.id.deal_price);
    TextView duration = (TextView)vi.findViewById(R.id.duration); 
    ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image);
    ImageButton imgb =  (ImageButton)vi.findViewById(R.idIMGB);

    HashMap<String, String> otherdeals = new HashMap<String, String>();
    otherdeals = data.get(position);

    title.setText(otherdeals.get(dealsparsing.TAG_TITLE));
    retail_price.setText(otherdeals.get(dealsparsing.TAG_RETAIL));
    retail_price.setPaintFlags(retail_price.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
    deal_price.setText(otherdeals.get(dealsparsing.TAG_DEAL));
   duration.setText(otherdeals.get(dealsparsing.TAG_FINAL_TIME));
   Bitmap bitmap = DownloadImage(otherdeals.get(dealsparsing.TAG_IMAGE_URL));
   thumb_image.setImageBitmap(bitmap);

    return vi;

} 

2 个答案:

答案 0 :(得分:3)

您可以像这样实现imageButton的案例逻辑。

if(case1)
{
 imgb.setImageResource(R.drawable.enableImage);
}
if(case2)
{
 imgb.setImageResource(R.drawable.disableImage);
}

答案 1 :(得分:2)

您需要定义自己的(自定义)列表适配器(如果没有)。在适配器的getView()方法中,您将按钮设置为启用/禁用并更改图像(取决于您的情况/条件)

编辑:您编辑了代码并添加了适配器的getView方法。现在问题在哪里?检查您的状况并将ImageButton设置为启用/禁用并更改图像