[英]Conditionally change imageButton image
我在列表视图中有一个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;
}
您可以像这样为imageButton实现大小写逻辑。
if(case1)
{
imgb.setImageResource(R.drawable.enableImage);
}
if(case2)
{
imgb.setImageResource(R.drawable.disableImage);
}
您需要定义自己的(自定义)列表适配器(如果尚未定义)。 在适配器的getView()方法中,将按钮设置为启用/禁用并更改图像(取决于情况/条件)
编辑:您编辑了代码,并添加了适配器的getView方法。 现在问题出在哪里? 检查您的状况并将ImageButton设置为启用/禁用并更改图像
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.