自定义适配器ViewHolder问题

自定义适配器ViewHolder问题

本文介绍了自定义适配器ViewHolder问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用自定义适配器列表。这份名单有2种类型的行,但我只用一个布局(保持我不能见度组需要 View.GONE 的项目)。不过,我一直试图进入编辑框式行时得到一个类转换异常。我有一个自定义适配器非常缺乏经验。您的帮助实在是AP preciated:D

这里的code(我删除了setonclicklisteners尽量简短):

 公共类SubEventListAdapter扩展ArrayAdapter< MobileSubEvent>
{    私人的ArrayList< MobileSubEvent> _items;
    私人语境_context;    公共SubEventListAdapter(上下文的背景下,ArrayList的< MobileSubEvent>的项目)
        {
            超(背景下,R.layout.view_select_event_item3,项目);
            this._items =物品;
            this._context =背景;
        }    静态类ViewHolder
        {
            TextView的文本;
            的ImageButton ImageButton的;
            ImageView的检查;
            EDITTEXT的EditText;
            Button按钮;
        }    @覆盖
    公众诠释getCount将()
        {
            返回this._items.size();
        }
    @覆盖
    公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup)
        {
            视图V = convertView;
            最后ViewHolder viewHolder;
            最后MobileSubEvent事件= _items.get(位置);            如果(V == NULL)
                {
                    LayoutInflater _inflater =(LayoutInflater)this.getContext()getSystemService(Context.LAYOUT_INFLATER_SERVICE)。
                    V = _inflater.inflate(R.layout.view_select_event_item3,NULL);
                    viewHolder =新ViewHolder();
                    viewHolder.imagebutton =(的ImageButton)v.findViewById(R.id.ibNext);
                    viewHolder.text =(TextView中)v.findViewById(R.id.EVENT);
                    viewHolder.check =(ImageView的)v.findViewById(R.id.ivCheck);
                    viewHolder.button =(按钮)v.findViewById(R.id.bScanOrSign);
                    viewHolder.editText =(EditText上)v.findViewById(R.id.etInput);                            v.setTag(viewHolder);                }其他
                {
                    V = convertView;
                     viewHolder =(ViewHolder)v.getTag(); //此处是类转换异常发生
                }
            如果(viewHolder.text!= NULL)
                viewHolder.text.setText(this._items.get(位置).get_description());
            v.setTag(this._items.get(位置));     ...
     返回伏;
        }

logcat的:

view_select_event_item3:

 <?XML版本=1.0编码=UTF-8&GT?;< RelativeLayout的
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:背景=@机器人:彩色/透明
    机器人:填充=5dip>    < ImageView的
        机器人:ID =@ + ID / ivCheck
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_gravity =center_vertical
        机器人:背景=@绘制/勾选
        机器人:可聚焦=假
        机器人:知名度=隐形/>    <的TextView
        机器人:ID =@ + ID /事件
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_toRightOf =@ ID / ivCheck
        机器人:背景=@机器人:彩色/透明
        机器人:填充=5DP
        机器人:文字=示例文本
        机器人:TEXTSIZE =20SP
        机器人:文字样式=大胆
        机器人:知名度=看得见
        />    <的EditText
        机器人:ID =@ + ID / etInput
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        安卓了minWidth =130dp
        安卓了maxWidth =165dp
        机器人:layout_toRightOf =@ ID / ivCheck
        机器人:背景=@机器人:可绘制/ editbox_background
        机器人:填充=5DP
        机器人:提示=提示文字
        机器人:单线=真
        机器人:文字=
        机器人:TEXTSIZE =20SP
        机器人:知名度=隐形
         />    <按钮
         机器人:ID =@ + ID / bScanOrSign
        机器人:layout_width =62dp
        机器人:layout_height =32dp
        机器人:layout_gravity =center_vertical
        机器人:layout_toLeftOf =@ ID / ibNext
        机器人:文字=扫描
        机器人:知名度=隐形
        />    <的ImageButton
        机器人:ID =@ + ID / ibNext
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentRight =真
        机器人:layout_gravity =右| center_vertical
        机器人:背景=@绘制/右
        机器人:知名度=隐形/>< / RelativeLayout的>


解决方案

试试这个在getView,检查convertView对象的类名(查看Holder对象),如果它不符合你的ViewHolder的类名(在我的情况下,它是VisitsListViewHolder) - 其设置为用于视图支架创建空,因此新的实例

 如果(convertView!= NULL)
    {
     字符串的className = convertView.getTag()的getClass()的getName()。
     如果(!className.equalsIgnoreCase(VisitsListViewHolder))
     {
         convertView = NULL;
     }
    }

I'm trying to use a custom adapter for a list. This list has 2 types of rows but I only use one layout (keeping the items that I don't need with visibility set to View.GONE). However I keep getting a Class cast exception when trying to access the editbox-style row. I have very little experience with custom adapters. Your help is really appreciated :D

Here's the code(I removed the setonclicklisteners to keep it short):

public class SubEventListAdapter extends ArrayAdapter<MobileSubEvent>
{

    private ArrayList<MobileSubEvent> _items;
    private Context _context;

    public SubEventListAdapter(Context context, ArrayList<MobileSubEvent> items)
        {
            super(context, R.layout.view_select_event_item3, items);
            this._items = items;
            this._context = context;
        }

    static class ViewHolder
        {
            TextView text;
            ImageButton imagebutton;
            ImageView check;
            EditText editText;
            Button button;
        }

    @Override
    public int getCount()
        {
            return this._items.size();
        }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
        {
            View v = convertView;
            final ViewHolder viewHolder;
            final MobileSubEvent event = _items.get(position);

            if (v == null)
                {
                    LayoutInflater _inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = _inflater.inflate(R.layout.view_select_event_item3, null);
                    viewHolder = new ViewHolder();
                    viewHolder.imagebutton = (ImageButton) v.findViewById(R.id.ibNext);
                    viewHolder.text = (TextView) v.findViewById(R.id.EVENT);
                    viewHolder.check = (ImageView) v.findViewById(R.id.ivCheck);
                    viewHolder.button = (Button) v.findViewById(R.id.bScanOrSign);
                    viewHolder.editText = (EditText) v.findViewById(R.id.etInput);

                            v.setTag(viewHolder);

                } else
                {
                    v  = convertView;
                     viewHolder = (ViewHolder) v.getTag(); //here is where the class cast exception occurs
                }


            if (viewHolder.text != null)
                viewHolder.text.setText(this._items.get(position).get_description());
            v.setTag(this._items.get(position));

     ...


     return v;
        }

Logcat:

view_select_event_item3:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:padding="5dip" >

    <ImageView
        android:id="@+id/ivCheck"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_gravity="center_vertical"
        android:background="@drawable/check"
        android:focusable="false"
        android:visibility="invisible" />

    <TextView
        android:id="@+id/EVENT"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/ivCheck"
        android:background="@android:color/transparent"
        android:padding="5dp"
        android:text="Sample text"
        android:textSize="20sp"
        android:textStyle="bold"
        android:visibility="visible"
        />

    <EditText
        android:id="@+id/etInput"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="130dp"
        android:maxWidth="165dp"
        android:layout_toRightOf="@id/ivCheck"
        android:background="@android:drawable/editbox_background"
        android:padding="5dp"
        android:hint="Hint text"
        android:singleLine="true"
        android:text=""
        android:textSize="20sp"
        android:visibility="invisible"
         />

    <Button
         android:id="@+id/bScanOrSign"
        android:layout_width="62dp"
        android:layout_height="32dp"
        android:layout_gravity="center_vertical"
        android:layout_toLeftOf="@id/ibNext"
        android:text="Scan"
        android:visibility="invisible"
        />

    <ImageButton
        android:id="@+id/ibNext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_gravity="right|center_vertical"
        android:background="@drawable/right"
        android:visibility="invisible" />

</RelativeLayout>
解决方案

Try this in the getView, check for class name of the convertView object (The View Holder object) if it does not matches class name of your ViewHolder (in my case it is VisitsListViewHolder) - set it as null so new instance is created for the view holder

if(convertView != null)
    {
     String className = convertView.getTag().getClass().getName();
     if(!className.equalsIgnoreCase("VisitsListViewHolder"))
     {
         convertView = null;
     }
    }

这篇关于自定义适配器ViewHolder问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 10:14