我有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