高效的适配器具有java.lang.ClassCastException吗? [英] Efficient adapter has java.lang.ClassCastException?
问题描述
观看此处之后,我尝试实现自己的高效适配器,
After watching here I try to implement my own efficient adapter,
我的ViewHolder类几乎相同:
My ViewHolder class almost same:
static class ViewHolder {
ImageButton button;
TextView txtView;
}
getView方法如下:
getView method look like:
private void getView(...) {
if(convertView == null) {
convertView = LayoutInflater.from(
parent.getContext()).inflate(R.layout.linear_container,
parent, false);
holder = new ViewHolder();
convertView.setTag(holder);
} else {
// erro line
holder = (ViewHolder) convertView.getTag();
}
LinearLayout llCustomImgViewContainer = (LinearLayout) convertView
.findViewById(R.id.llContainer);
llCustomImgViewContainer.setTag(viewPosition);
return converView;
}
但是这里一旦开始绘制新视图,就会给我错误
but here once new view started to draw, it give me error
D/AndroidRuntime(748):关闭VM W/dalvikvm(748):threadid = 1:线程以未捕获的异常退出(group = 0x412a4300) E/AndroidRuntime(748):致命异常:主要 E/AndroidRuntime(748): java.lang.ClassCastException:java.lang.Integer无法转换为com.droid.test.widget.customListView $ CustomBaseAdapter $ ViewHolder
D/AndroidRuntime( 748): Shutting down VM W/dalvikvm( 748): threadid=1: thread exiting with uncaught exception (group=0x412a4300) E/AndroidRuntime( 748): FATAL EXCEPTION: main E/AndroidRuntime( 748): java.lang.ClassCastException: java.lang.Integer cannot be cast to com.droid.test.widget.customListView$CustomBaseAdapter$ViewHolder
有人知道这是怎么回事吗?
any one have idea what is wrong here?
推荐答案
似乎一开始就是
convertView.setTag(holder);
您要设置与此视图关联的标签(是holder),但稍后使用
line you are setting the tag(which is holder) associated with this view but later with
llCustomImgViewContainer.setTag(viewPosition);
您正在将viewPosition设置为标签.然后可能在
you are setting viewPosition as a tag. Then probably in
holder = (ViewHolder) convertView.getTag();
您的代码尝试将Integer转换为ViewHolder并引发java.lang.ClassCastException.
your code trying to cast Integer to ViewHolder and throws a java.lang.ClassCastException.
如果我没记错,这就是"linear_container"布局的结构
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>
视图从
LayoutInflater.from(parent.getContext()).inflate(R.layout.linear_container,parent,false);
和从
convertView.findViewById(R.id.llContainer);
应该相同.
这篇关于高效的适配器具有java.lang.ClassCastException吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!