2011-04-12 66 views
5

我有ListView與2種不同的佈局:一個與圖像和另一個沒有圖像。我嘗試做類似this。我重寫BaseAdapter的getView:ViewHolders ListViews與不同的項目佈局

public View getView(int position, View convertView, ViewGroup parent) { 
     Map<String, String> item = mData.get(position); 
     if(item.get("image_location").equals("") == true){ 
      ViewHolderWithoutImage holder = new ViewHolderWithoutImage(); 
      if(convertView == null){ 
       convertView = mInflater.inflate(R.layout.row_without_image, null); 
       holder.title = (TextView)convertView.findViewById(R.id.title); 
       holder.firstParagraph = (TextView)convertView.findViewById(R.id.first_paragraph); 
       convertView.setTag(holder); 
      }else{ 
       holder = (ViewHolderWithoutImage)convertView.getTag(); 
      } 
      holder.title.setText(mData.get(position).get("title").toString()); 
      holder.firstParagraph.setText(item.get("first_paragraph").toString()); 

     }else{ 
      ViewHolderWithImage holder = new ViewHolderWithImage(); 
      Bitmap bm = null; 
      if(convertView == null){ 
       convertView = mInflater.inflate(R.layout.row_with_image, null); 
       holder.title = (TextView)convertView.findViewById(R.id.title_image); 
       holder.firstParagraph = (TextView)convertView.findViewById(R.id.first_paragraph_image); 
       holder.image = (ImageView)convertView.findViewById(R.id.image); 
       convertView.setTag(holder); 
      }else{ 
       holder = (ViewHolderWithImage)convertView.getTag(); 
      } 

      holder.title.setText(mData.get(position).get("title").toString()); 
      holder.firstParagraph.setText(item.get("first_paragraph").toString()); 
      String location = imageBaseUrl + item.get("image_location"); 
      bm = downloadImage(location); 
      holder.image.setImageBitmap(bm); 
     } 
     return convertView; 
    } 

我ViewHolders類:

static class ViewHolderWithImage { 
     TextView title; 
     TextView firstParagraph; 
     ImageView image; 
    } 

    static class ViewHolderWithoutImage { 
     TextView title; 
     TextView firstParagraph; 
    } 

它的工作原理沒有第二部分,但崩潰時它會

holder = (ViewHolderWithImage)convertView.getTag(); 

部分時item.get("image_location").equals("") != truejava.lang.reflect.InvocationTargetException 。任何想法如何解決?

+1

嘗試使用getCause()或getTargetException()(請參閱:http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/InvocationTargetException.html)以獲取更多信息關於例外。 – MByD 2011-04-12 12:03:06

回答

19

我認爲這是因爲convertView的標記包含不同類型的ViewHolder。嘗試檢查convertView類型:

if(item.get("image_location").equals("") == true){ 
    ... 
    if(convertView == null || !(convertView.getTag() instanceof ViewHolderWithoutImage)){ 
    ... 
}else{ 
    ... 
    if(convertView == null || !(convertView.getTag() instanceof ViewHolderWithImage)){ 
     convertView = mInflater.inflate(R.layout.row_with_image, null); 
     ... 

P.S.最好使用系統方法處理不同的佈局(覆蓋getItemViewType())。 There is good article on this topic

+0

真是一個偉大的想法。我的問題解決了。 – Salim 2014-02-03 08:31:15

+0

該文章的最後一段代碼不正確。 他沒有管理這種情況,例如: convertView!= null,目前instanciated爲R.layout.item1,但當前位置需要R.layout.item2 – 2015-06-09 19:34:53

1

你應該在你的適配器覆蓋getItemViewType()getViewTypeCount(),爲ViewHolderWithoutImage和1 ViewHolderWithImage返回不同數量的每一行的類型,比如0。這樣,getView()可以正確解析哪個視圖實例化。