0

我在下面粘贴了我的代码,这个概念是我有 3 种不同类型的列表视图项,可以被查看器类看到。现在,当我滚动列表视图时,它崩溃了,freeTextHolder = (ViewHolderFreeText) rowView.getTag();因为它_position与所需的位置对象不匹配,itemList这表明它的 a"free text"if (itemList.get(_position).getType().compareTo(Question.TYPE_CATEGORY) == 0)真的,但rowView.getTag()显示了它的复选框,所以_position1 增加了所需的位置,然后它崩溃了。

    @Override
public View getView(final int _position, View _convertView,
        ViewGroup _parent) {

    View rowView = _convertView;
    if (itemList.get(_position).getType().compareTo(Question.TYPE_CATEGORY) == 0) {
        ViewHolderBranch branchHolder = new ViewHolderBranch();

        // Compartment for Branch
        if (rowView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflater.inflate(R.layout.listview_branch, _parent,
                    false);

            branchHolder.hTextTitle = (TextView) rowView
                    .findViewById(R.id.textTitleB);
            branchHolder.hTextDetail = (TextView) rowView
                    .findViewById(R.id.textDetailB);
            branchHolder.hIcon = (ImageView) rowView
                    .findViewById(R.id.imageArrow);

            rowView.setTag(branchHolder);
        } else {

            branchHolder = (ViewHolderBranch) rowView.getTag();
        }

        branchHolder.hTextTitle.setText(itemList.get(_position).getText());
        if (itemList.get(_position).isIsReadCode())
            branchHolder.hTextDetail.setText(itemList.get(_position)
                    .getReadCode());
        else
            branchHolder.hTextDetail.setText(" ");
        branchHolder.hIcon
                .setImageResource(android.R.drawable.ic_media_play);

        // return rowView;
    }

    else if (itemList.get(_position).getType()
            .compareTo(Question.TYPE_CHECKBOX) == 0) {
        // Compartment for Checkboxes

        ViewHolderCheckbox checkboxHolder = new ViewHolderCheckbox();

        // Compartment for Branch
        if (rowView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflater.inflate(R.layout.listview_checkbox, _parent,
                    false);

            checkboxHolder.hTextTitle = (TextView) rowView
                    .findViewById(R.id.textTitleCB);
            checkboxHolder.hTextDetail = (TextView) rowView
                    .findViewById(R.id.textDetailCB);
            checkboxHolder.hCheckBox = (CheckBox) rowView
                    .findViewById(R.id.checkBox);
            rowView.setTag(checkboxHolder);
        } else {

            checkboxHolder = (ViewHolderCheckbox) rowView.getTag();
        }

        checkboxHolder.hTextTitle
                .setText(itemList.get(_position).getText());
        if (itemList.get(_position).isIsReadCode())
            checkboxHolder.hTextDetail.setText(itemList.get(_position)
                    .getReadCode());
        else
            checkboxHolder.hTextDetail.setText(" ");
        checkboxHolder.hCheckBox.setChecked(itemList.get(_position)
                .isChecked());

        // return rowView;

    }

    if (itemList.get(_position).getType().compareTo(Question.TYPE_FREETEXT) == 0) {
        ViewHolderFreeText freeTextHolder = new ViewHolderFreeText();

        // Compartment for Branch
        if (rowView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflater.inflate(R.layout.listview_button, _parent,
                    false);

            freeTextHolder.hTextTitle = (TextView) rowView
                    .findViewById(R.id.textTitleBtn);
            freeTextHolder.hTextDetail = (TextView) rowView
                    .findViewById(R.id.textDetailBtn);
            freeTextHolder.hIcon = (ImageView) rowView
                    .findViewById(R.id.imagePlusBtn);

            rowView.setTag(freeTextHolder);
        } else {

            freeTextHolder = (ViewHolderFreeText) rowView.getTag();
        }

        freeTextHolder.hTextTitle.setText(itemList.get(_position).getText());
        if (itemList.get(_position).isIsReadCode())
            freeTextHolder.hTextDetail.setText(itemList.get(_position)
                    .getReadCode());
        else
            freeTextHolder.hTextDetail.setText(" ");
        freeTextHolder.hIcon
                .setImageResource(android.R.drawable.ic_input_add);

        // return rowView;
    }

    return rowView;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return itemList.size();
}

@Override
public Object getItem(int _position) {
    // TODO Auto-generated method stub
    return _position;
}

@Override
public long getItemId(int _position) {
    // TODO Auto-generated method stub
    return _position;
}

并且视图持有者类是

静态类 ViewHolderBranch { TextView hTextTitle; 文本视图 hTextDetail; ImageView hIcon; }

static class ViewHolderFreeText
{
    TextView hTextTitle;
    TextView hTextDetail;
    ImageView hIcon;
}

// View Holder for Checkbox Items
/**
 * Class for holding different view items
 * 
 */
static class ViewHolderCheckbox {
    TextView hTextTitle;
    TextView hTextDetail;
    CheckBox hCheckBox;
}

痕迹

  09-23 15:36:29.105: D/AndroidRuntime(8908): Shutting down VM 09-23 15:36:29.105: W/dalvikvm(8908): threadid=1: thread exiting with
 uncaught exception (group=0x40014760) 09-23 15:36:29.326:
 E/AndroidRuntime(8908): FATAL EXCEPTION: main 09-23 15:36:29.326:
 E/AndroidRuntime(8908): java.lang.ClassCastException:
 com.techmelo.ws.CustomizeLViewAdapter$ViewHolderBranch cannot be cast
 to com.techmelo.ws.CustomizeLViewAdapter$ViewHolderFreeText 09-23
 15:36:29.326: E/AndroidRuntime(8908):  at
 com.techmelo.ws.CustomizeLViewAdapter.getView(CustomizeLViewAdapter.java:157)
 09-23 15:36:29.326: E/AndroidRuntime(8908):    at
 android.widget.AbsListView.obtainView(AbsListView.java:1943) 09-23
 15:36:29.326: E/AndroidRuntime(8908):  at
 android.widget.ListView.makeAndAddView(ListView.java:1756) 09-23
 15:36:29.326: E/AndroidRuntime(8908):  at
 android.widget.ListView.fillDown(ListView.java:656) 09-23
 15:36:29.326: E/AndroidRuntime(8908):  at
 android.widget.ListView.fillGap(ListView.java:620) 09-23 15:36:29.326:
 E/AndroidRuntime(8908):    at
 android.widget.AbsListView.trackMotionScroll(AbsListView.java:4350)
 09-23 15:36:29.326: E/AndroidRuntime(8908):    at
 android.widget.AbsListView.onTouchEvent(AbsListView.java:2892) 09-23
 15:36:29.326: E/AndroidRuntime(8908):  at
 android.widget.ListView.onTouchEvent(ListView.java:3566) 09-23
 15:36:29.326: E/AndroidRuntime(8908):  at
 android.view.View.dispatchTouchEvent(View.java:4600) 09-23
 15:36:29.326: E/AndroidRuntime(8908):  at
 android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1488)
 09-23 15:36:29.326: E/AndroidRuntime(8908):    at
 android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1256) 09-23
 15:36:29.326: E/AndroidRuntime(8908):  at
 android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1494)
 09-23 15:36:29.326: E/AndroidRuntime(8908):    at
 android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1269) 09-23
 15:36:29.326: E/AndroidRuntime(8908):  at
 android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1494)
 09-23 15:36:29.326: E/AndroidRuntime(8908):    at
 android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1269) 09-23
 15:36:29.326: E/AndroidRuntime(8908):  at
 android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1494)
 09-23 15:36:29.326: E/AndroidRuntime(8908):    at
 android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1269) 09-23
 15:36:29.326: E/AndroidRuntime(8908):  at
 android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1494)
 09-23 15:36:29.326: E/AndroidRuntime(8908):    at
 android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1269) 09-23
 15:36:29.326: E/AndroidRuntime(8908):  at
 android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1494)
 09-23 15:36:29.326: E/AndroidRuntime(8908):    at
 android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1269) 09-23
 15:36:29.326: E/AndroidRuntime(8908):  at
 android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1494)
 09-23 15:36:29.326: E/AndroidRuntime(8908):    at
 android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1269) 09-23
 15:36:29.326: E/AndroidRuntime(8908):  at
 android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1494)
 09-23 15:36:29.326: E/AndroidRuntime(8908):    at
 android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1269) 09-23
 15:36:29.326: E/AndroidRuntime(8908):  at
 com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1700)
 09-23 15:36:29.326: E/AndroidRuntime(8908):    at
 com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1270)
 09-23 15:36:29.326: E/AndroidRuntime(8908):    at
 android.app.Activity.dispatchTouchEvent(Activity.java:2277) 09-23
 15:36:29.326: E/AndroidRuntime(8908):  at
 com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1680)
 09-23 15:36:29.326: E/AndroidRuntime(8908):    at
 android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2272) 09-23
 15:36:29.326: E/AndroidRuntime(8908):  at
 android.view.ViewRoot.handleMessage(ViewRoot.java:1958) 09-23
 15:36:29.326: E/AndroidRuntime(8908):  at
 android.os.Handler.dispatchMessage(Handler.java:99) 09-23
 15:36:29.326: E/AndroidRuntime(8908):  at
 android.os.Looper.loop(Looper.java:126) 09-23 15:36:29.326:
 E/AndroidRuntime(8908):    at
 android.app.ActivityThread.main(ActivityThread.java:3997) 09-23
 15:36:29.326: E/AndroidRuntime(8908):  at
 java.lang.reflect.Method.invokeNative(Native Method) 09-23
 15:36:29.326: E/AndroidRuntime(8908):  at
 java.lang.reflect.Method.invoke(Method.java:491) 09-23 15:36:29.326:
 E/AndroidRuntime(8908):    at
 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
 09-23 15:36:29.326: E/AndroidRuntime(8908):    at
 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 09-23
 15:36:29.326: E/AndroidRuntime(8908):  at
 dalvik.system.NativeStart.main(Native Method) 09-23 15:36:39.334:
 W/ActivityThread(9001): Application com.techmelo.ws is waiting for the
 debugger on port 8100... 09-23 15:36:39.345: I/System.out(9001):
 Sending WAIT chunk 09-23 15:36:39.364: I/dalvikvm(9001): Debugger is
 active 09-23 15:36:39.554: I/System.out(9001): Debugger has connected
 09-23 15:36:39.554: I/System.out(9001): waiting for debugger to
 settle... 09-23 15:36:39.808: I/System.out(9001): waiting for debugger
 to settle... 09-23 15:36:40.014: I/System.out(9001): waiting for
 debugger to settle... 09-23 15:36:40.216: I/System.out(9001): waiting
 for debugger to settle... 09-23 15:36:40.416: I/System.out(9001):
 waiting for debugger to settle... 09-23 15:36:40.647:
 I/System.out(9001): waiting for debugger to settle... 09-23
 15:36:40.884: I/System.out(9001): waiting for debugger to settle...
 09-23 15:36:41.085: I/System.out(9001): waiting for debugger to
 settle... 09-23 15:36:41.299: I/System.out(9001): waiting for debugger
 to settle... 09-23 15:36:41.524: I/System.out(9001): waiting for
 debugger to settle... 09-23 15:36:41.755: I/System.out(9001): debugger
 has settled (1390) 09-23 15:36:42.994: D/dalvikvm(9001): GC_FOR_ALLOC
 freed 63K, 4% free 6386K/6599K, paused 45ms 09-23 15:36:43.004:
 I/dalvikvm-heap(9001): Grow heap (frag case) to 6.820MB for
 513744-byte allocation 09-23 15:36:43.284: D/dalvikvm(9001):
 GC_CONCURRENT freed <1K, 4% free 6888K/7111K, paused 29ms+2ms 09-23
 15:36:46.084: V/TLINE(9001): new: android.text.TextLine@40719db0 09-23
 15:36:47.904: V/TLINE(9001): new: android.text.TextLine@4072b720 09-23
 15:37:11.404: D/dalvikvm(9001): threadid=1: still suspended after undo
 (sc=1 dc=1) 09-23 15:37:11.414: D/dalvikvm(9001): GC_CONCURRENT freed
 117K, 4% free 7263K/7495K, paused 13ms+8ms
4

1 回答 1

2

发生异常是因为您的不同代码分支(针对不同类型的行)可能都在处理一个重复使用的行视图,该行视图具有与之关联的任何类型的标记对象。

最好的解决方案是覆盖Adapter'sgetItemViewType(int)getViewTypeCount()方法以指定您拥有多少种不同类型的项目视图并将它们映射到列表位置。这将简化您的逻辑,getView(...)并让ListView不同视图类型以不同方式处理行回收池。

您可以将测试更改getView(...)为:

if (getItemViewType(_position) == TYPE_CATEGORY) {
   // code for categories
}

您的 Adapter 子类知道TYPE_CATEGORY的 an在哪里。int

于 2012-09-26T02:57:14.627 回答