2012-03-20 33 views
1

我有一個imageButton在一個列表視圖,我想改變它的形象取決於兩種情況。在第一種情況下,圖像按鈕被啓用並且具有圖像。在第二種情況下,圖像按鈕被禁用,並應具有不同的圖像。有條件地改變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

您需要定義自己的(自定義)列表適配器(如果沒有)。在適配器的getView()方法中,您設置了您的按鈕啓用/ disbabled和更改圖像(取決於您的情況/條件)

編輯:您編輯您的代碼並添加您的適配器的getView方法。現在問題在哪裏?檢查你的條件,並將ImageButton設置爲啓用/禁用並更改圖像

3

您可以像這樣實現您的imageButton的案例邏輯。

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

我試圖使用這個參數如果(參與者> max_commands)裏面的getView但日食顯示我的錯誤 – 2012-03-22 11:55:03