Custom Adapter ViewHolder issue

旧街凉风 提交于 2019-12-08 13:03:25

问题


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>

回答1:


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;
     }
    }


来源:https://stackoverflow.com/questions/17649719/custom-adapter-viewholder-issue

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