2010-05-26 87 views
1

我有一個列表視圖,其中包括2 textview和1 imageview。現在,imageview中的圖像將在特定條件下設置,否則它應該留空而不顯示圖像。如何在Android中的ListView中的imageview中設置圖像?

問題是圖像沒有進入正確的行。說,例如圖像應該顯示在第3行,但它顯示在第4行。並且,在某些情況下,每行將具有相同的圖像。

有人遇到過這樣的問題嗎?我對這背後的原因無能爲力。任何人都可以指出問題嗎?


private static class VilleAdapter extends BaseAdapter { 
    private LayoutInflater mInflater = null; 

    // private Context context = null; 

    public VilleAdapter(Context context) { 
     // Cache the LayoutInflate to avoid asking for a new one each time. 
     mInflater = LayoutInflater.from(context); 
    } 

    /** 
    * The number of items in the list is determined by the number of 
    * speeches in our array. 
    * 
    * @see android.widget.ListAdapter#getCount() 
    */ 
    public int getCount() { 
     // return BabbleMainListParse.getNumOfBabbles(); 
     return VilleMainListParse.getVilleCount(); 
    } 

    /** 
    * Since the data comes from an array, just returning the index is 
    * sufficent to get at the data. If we were using a more complex data 
    * structure, we would return whatever object represents one row in the 
    * list. 
    * 
    * @see android.widget.ListAdapter#getItem(int) 
    */ 
    public Object getItem(int position) { 
     return position; 
    } 

    /** 
    * Use the array index as a unique id. 
    * 
    * @see android.widget.ListAdapter#getItemId(int) 
    */ 
    public long getItemId(int position) { 
     return position; 
    } 

    /** 
    * Make a view to hold each row. 
    * 
    * @see android.widget.ListAdapter#getView(int, android.view.View, 
    *  android.view.ViewGroup) 
    */ 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     // A ViewHolder keeps references to children views to avoid 
     // unneccessary calls 
     // to findViewById() on each row. 
     ViewHolder holder; 

     // When convertView is not null, we can reuse it directly, there is 
     // no need 
     // to reinflate it. We only inflate a new View when the convertView 
     // supplied 
     // by ListView is null. 
     if (convertView == null) 
     { 
      convertView = mInflater.inflate(R.layout.villerow, parent,false); 

      // Creates a ViewHolder and store references to the two children 
      // views 
      // we want to bind data to. 
      holder = new ViewHolder(); 
      holder.txtVilleListTitle = (TextView) convertView.findViewById(R.id.txtVilleListTitle); 
      holder.txtVilleDescription = (TextView) convertView.findViewById(R.id.txtVilleDescription); 
      holder.imgvillepwd = (ImageView) convertView.findViewById(R.id.imgvillepwd); 

      convertView.setTag(holder); 
     } 
     else 
     { 
      // Get the ViewHolder back to get fast access to the TextView 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     // Bind the data efficiently with the holder. 
     holder.txtVilleListTitle.setText(VilleMainListParse.getMsgTitle(position)); 

     if(VilleMainListParse.getMsgDesc(position).length() > 30) 
     { 
      String temp = VilleMainListParse.getMsgDesc(position).substring(0, 30); 
      temp = temp +"....."; 
      holder.txtVilleDescription.setText(temp); 
     } 
     else 
      holder.txtVilleDescription.setText(VilleMainListParse.getMsgDesc(position)); 

     if(!VilleMainListParse.getPwd(position).equals("")) 
     { 
      if(VilleMainListParse.getUnlock(position)) 
      { 
       holder.imgvillepwd.setBackgroundResource(R.drawable.lock); 
      } 
      else if(!VilleMainListParse.getUnlock(position)) 
      { 
       holder.imgvillepwd.setBackgroundResource(R.drawable.lock_open); 
      } 
     } 

     return convertView; 
    } 

    static class ViewHolder 
    { 
     TextView txtVilleListTitle; 
     TextView txtVilleDescription; 
     ImageView imgvillepwd; 
    } 
} 

上面的代碼僅是適配器的。當登錄按鈕被點擊時,最初有一個登錄屏幕,數據從互聯網上載入,然後這個列表視圖被載入這個數據。該數據在最初加載正確。

有人可以讓我知道這個代碼有什麼問題嗎?

回答

1

由於您正在重複使用相同的視圖,因此您應該清除不應顯示的圖像。否則,先前的圖片將顯示在那裏。所以我建議以下修復:

if(!VilleMainListParse.getPwd(position).equals("")) 
{ 
    if(VilleMainListParse.getUnlock(position)) 
    { 
     holder.imgvillepwd.setBackgroundResource(R.drawable.lock); 
    } 
    else if(!VilleMainListParse.getUnlock(position)) 
    { 
     holder.imgvillepwd.setBackgroundResource(R.drawable.lock_open); 
    } 
} 
else 
    holder.imgvillepwd.setBackgroundDrawable(null); 
+0

啊!我是這麼想的。但我沒有把它設置爲空。我實際上對GONE的可見度有所提高,但這也導致了一些奇怪的結果。無論如何,將背景設置爲null真的很成功。我認爲該方法應該是setBackgroundDrawable(null)。反正非常感謝。 – sunil 2010-05-26 10:37:20

0

您也可以考慮使用setImageResource/setImageBitmap方法而不是setBackgroundXxx。

相關問題