问题
I have an imageButton inside a listview and I want to change its image depending on two cases. In the first case the imagebutton is enabled and has an image. In the second case the imagebutton is disabled and should have a different image.
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;
}
回答1:
You can implement your case logic for imageButton like this.
if(case1)
{
imgb.setImageResource(R.drawable.enableImage);
}
if(case2)
{
imgb.setImageResource(R.drawable.disableImage);
}
回答2:
You need to define your own (custom) list adapter (if you haven't). In adapter's getView() method you set your button enabled/disbabled and change the image (depending on your case/condition)
Edit: You edited your code and added your adapter's getView method. Now where is the problem? Check your condition and set the ImageButton to enabled/disabled and change the image
来源:https://stackoverflow.com/questions/9795418/conditionally-change-imagebutton-image