sinat_33162446 2015-11-27 07:09 采纳率: 0%
浏览 1981

关于convertView复用的问题

解析数据一共有两种,有一种带图片有一种不带图片,故用了两套布局,流程和正常ListView加载多套布局一样复用convertView然后setTag,但是一运行就会出错,详见下面:

Adapter中getView方法代码如下:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
int type = list.get(position).getType();
ViewHolder3 vh3 = null;
ViewHolder vh = null;
Picasso p = Picasso.with(context);
if (convertView == null) {
switch (type) {
case 0:
convertView = LayoutInflater.from(context).inflate(R.layout.item_social_lv_noimage, null);
vh = new ViewHolder();
vh.iv_head = (ImageView) convertView.findViewById(R.id.iv_item_no_head);
vh.tv_name = (TextView) convertView.findViewById(R.id.tv_item_no_name);
vh.tv_date = (TextView) convertView.findViewById(R.id.tv_item_no_date);
vh.tv_detail = (TextView) convertView.findViewById(R.id.tv_item_no_detail);
vh.tv_theme = (TextView) convertView.findViewById(R.id.tv_item_no_theme);
vh.tv_eye = (TextView) convertView.findViewById(R.id.tv_item_no_eye);
vh.tv_comments = (TextView) convertView.findViewById(R.id.tv_item_no_comments);
vh.tv_good = (TextView) convertView.findViewById(R.id.tv_item_no_good);
convertView.setTag(vh);
break;
case 1:
convertView = LayoutInflater.from(context).inflate(R.layout.item_social_lv_oneimage, null);
vh3 = new ViewHolder3();
vh3.iv_head = (ImageView) convertView.findViewById(R.id.iv_item_one_head);
vh3.iv_main1 = (ImageView) convertView.findViewById(R.id.iv_item_one_main);
vh3.tv_name = (TextView) convertView.findViewById(R.id.tv_item_one_name);
vh3.tv_date = (TextView) convertView.findViewById(R.id.tv_item_one_date);
vh3.tv_detail = (TextView) convertView.findViewById(R.id.tv_item_one_detail);
vh3.tv_theme = (TextView) convertView.findViewById(R.id.tv_item_one_theme);
vh3.tv_eye = (TextView) convertView.findViewById(R.id.tv_item_one_eye);
vh3.tv_comments = (TextView) convertView.findViewById(R.id.tv_item_one_comments);
vh3.tv_good = (TextView) convertView.findViewById(R.id.tv_item_one_good);
convertView.setTag(vh3);
break;
default:
break;
}
} else {
switch (type) {
case 0:
vh = (ViewHolder) convertView.getTag();
break;
case 1:
vh3 = (ViewHolder3) convertView.getTag();
break;
default:
break;
}
}
switch (type) {
case 0:
CommentBean comm = (CommentBean) list.get(position);
vh.tv_name.setText(comm.getAccountName());
String time = comm.getUserTime().replace("T", " ");
vh.tv_date.setText(time);
vh.tv_detail.setText(comm.getContent());
String theme = comm.getTitle();
vh.tv_theme.setText("#" + theme);
vh.tv_eye.setText(comm.getClickCount());
vh.tv_comments.setText(comm.getReplyCount());
vh.tv_good.setText(comm.getFavourableCount());
String img_head = comm.getIconPath();
p.load(img_head).error(R.mipmap.ic_launcher).into(vh.iv_head);
break;
case 1:
CommentBean3 comm3 = (CommentBean3) list.get(position);
vh3.tv_name.setText(comm3.getAccountName());
String time3 = comm3.getUserTime().replace("T", " ");
vh3.tv_date.setText(time3);
vh3.tv_detail.setText(comm3.getContent());
String theme3 = comm3.getTitle();
vh3.tv_theme.setText("#" + theme3);
vh3.tv_eye.setText(comm3.getClickCount());
vh3.tv_comments.setText(comm3.getReplyCount());
vh3.tv_good.setText(comm3.getFavourableCount());
String img_head3 = comm3.getIconPath();
p.load(img_head3).error(R.mipmap.ic_launcher).into(vh3.iv_head);
break;
default:
break;
}

return convertView;

}

然而报错
代码:
Process: com.ok.starfinder, PID: 9121
java.lang.ClassCastException: com.ok.starfinder.adapter.SquareListAdapter$ViewHolder3 cannot be cast to com.ok.starfinder.adapter.SquareListAdapter$ViewHolder
at com.ok.starfinder.adapter.SquareListAdapter.getView(SquareListAdapter.java:124)
at android.widget.AbsListView.obtainView(AbsListView.java:2240)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
at android.widget.ListView.onMeasure(ListView.java:1175)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
at android.view.View.measure(View.java:16497)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1940)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1137)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1319)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1024)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5694)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5045)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)

希望有大神解决一下,急

  • 写回答

1条回答 默认 最新

  • 有雄心的小菜鸟 2015-11-27 07:40
    关注

    java.lang.ClassCastException: com.ok.starfinder.adapter.SquareListAdapter$ViewHolder3 cannot be cast to com.ok.starfinder.adapter.SquareListAdapter$ViewHolder
    at com.ok.starfinder.adapter.SquareListAdapter.getView(SquareListAdapter.java:124)
    你看看124行有啥问题,看错误提示是类型转换出错了。

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体