我正在尝试将自定义适配器用于列表。此列表有 2 种类型的行,但我只使用一种布局(保留我不需要的项目并将可见性设置为View.GONE
)。但是,在尝试访问编辑框样式的行时,我不断收到 Class cast 异常。我对自定义适配器的经验很少。非常感谢您的帮助:D
这是代码(我删除了 setonclicklisteners 以保持简短):
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;
}
日志猫:
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>