具有不同项目布局的 ListView 的 ViewHolder
我有 ListView 有 2 种不同类型的布局:一种带有图像,另一种没有图像。我尝试做类似这个的事情。我重写了 BaseAdapter 的 getView:
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.InitationTargetException 。有什么想法可以解决这个问题吗?
I have ListView with 2 different types of layout: one with image and another without image. I try to do something like this. I override getView of BaseAdapter:
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;
}
My ViewHolders classes:
static class ViewHolderWithImage {
TextView title;
TextView firstParagraph;
ImageView image;
}
static class ViewHolderWithoutImage {
TextView title;
TextView firstParagraph;
}
It works without the second part, but crashes when it going to
holder = (ViewHolderWithImage)convertView.getTag();
in part when item.get("image_location").equals("") != true
with java.lang.reflect.InvocationTargetException
. Any ideas how can I fix that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为发生这种情况是因为convertView的标签保存了不同类型的ViewHolder。尝试检查
convertView
类型:PS 最好使用系统方法来处理不同的布局(覆盖
getItemViewType()
)。 关于这个主题有一篇很好的文章。I think it's happens because convertView's tag holds ViewHolder with different type. Try to check
convertView
type:P.S. It's better to use system methods for handling different layouts (override
getItemViewType()
). There is good article on this topic.您应该重写适配器上的
getItemViewType()
和getViewTypeCount()
,为每种行类型返回一个不同的数字,例如ViewHolderWithoutImage
为 0,ViewHolderWithoutImage 为 1对于ViewHolderWithImage
。这样,getView()
就可以正确解析要实例化的视图。You should override
getItemViewType()
andgetViewTypeCount()
on your Adapter, returning a distinct number for each row type, like 0 forViewHolderWithoutImage
and 1 forViewHolderWithImage
. This way,getView()
could resolve correctly which view to instantiate.