Efficient adapter has java.lang.ClassCastException?

纵然是瞬间 提交于 2019-12-06 14:08:16

It seems that at first with

convertView.setTag(holder);

line you are setting the tag(which is holder) associated with this view but later with

llCustomImgViewContainer.setTag(viewPosition);

you are setting viewPosition as a tag. Then probably in

holder = (ViewHolder) convertView.getTag();

your code trying to cast Integer to ViewHolder and throws a java.lang.ClassCastException.

If I'm not wrong and this is the structure of the "linear_container" layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/llContainer">

    <!-- some views -->

</LinearLayout>

The view returned from

LayoutInflater.from(parent.getContext()).inflate(R.layout.linear_container,parent,false);

and the view returned from

convertView.findViewById(R.id.llContainer);

should be same.

You're setting an int instead of the view :

llCustomImgViewContainer.setTag(viewPosition);

When you use setTag, you save the given object (viewPosition), in the calling object (llCustomImgViewContainer).

Remove the last line :

llCustomImgViewContainer.setTag(viewPosition);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!