我有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("") != true
與java.lang.reflect.InvocationTargetException
。任何想法如何解決?
嘗試使用getCause()或getTargetException()(請參閱:http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/InvocationTargetException.html)以獲取更多信息關於例外。 – MByD 2011-04-12 12:03:06