0
我試圖使用自定義適配器的列表。此列表有兩種類型的行,但我只使用一種佈局(將可見性不需要的項目保留爲View.GONE
)。但是,當我試圖訪問編輯框風格的行時,我不斷收到一個類轉換異常。我對自定義適配器的使用經驗不多。你的幫助是非常讚賞:d自定義適配器ViewHolder問題
下面的代碼(我刪除了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;
}
的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>
「editbox樣式的行」? –
發佈錯誤日誌! –
@AhmadKayyali,我的意思是有一個edittext字段的行 – Sebek