具有多个项类型的ListAdapter会抛出ClassCastException

时间:2013-04-06 21:45:07

标签: android listview classcastexception baseadapter

我正在为一个活动构建一个应用程序,其中一个功能是留言板。为此,我构建了一个ListView并加载了消息。

我有两种类型的消息:您发送的消息和另一封发送的消息。布局略有不同,U使用其他XML文件。我尝试了我的代码(下面),如果所有项目属于同一类型(您的或其他项目),它可以正常工作。但是当类型发生变化时,我得到ClassCastException。我知道这意味着什么,以及它出错的地方(项目持有者是不同的类,它试图使类的对象不是)但不知道如何解决它。

这是我的代码,这是我的ChatAdapter的getView。 ChatAdapter是一个扩展BaseAdapter的类。

public View getView(final int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    final Message m = messagelist.get(position);
    OwnMessageHolder ownMessageHolder;
    OtherMessageHolder otherMessageHolder;
    if(isSelf(m.getPerson().getId())) {
    if (vi == null) { //convertview==null
        ownMessageHolder = new OwnMessageHolder();
        vi = inflater.inflate(R.layout.listitem_chat_own, null);
        vi.setOnClickListener(null);
        vi.setOnLongClickListener(null);
        vi.setLongClickable(false);
        ownMessageHolder.name = (TextView) vi.findViewById(R.id.tv_chat_own_name);
        ownMessageHolder.message = (TextView) vi.findViewById(R.id.tv_chat_own_message);
        vi.setTag(ownMessageHolder);
    }else{//convertview is not null
        ownMessageHolder = (OwnMessageHolder)vi.getTag();
    }

    ownMessageHolder.name.setText(m.getPerson().getName());
    ownMessageHolder.message.setText(m.getMessages()[0]);

    vi.setFocusable(false);
    vi.setClickable(true);

    return vi;
    }else {//other
        if (vi == null) { //convertview==null
            otherMessageHolder = new OtherMessageHolder();
            vi = inflater.inflate(R.layout.listitem_chat_other, null);
            vi.setOnClickListener(null);
            vi.setOnLongClickListener(null);
            vi.setLongClickable(false);
            otherMessageHolder.name = (TextView) vi.findViewById(R.id.tv_chat_other_name);
            otherMessageHolder.message = (TextView) vi.findViewById(R.id.tv_chat_other_message);
            vi.setTag(otherMessageHolder);
        }else{//convertview is not null
            otherMessageHolder = (OtherMessageHolder)vi.getTag();
        }


        otherMessageHolder.name.setText(m.getPerson().getName());
        otherMessageHolder.message.setText(m.getMessages()[0]);

        vi.setFocusable(false);
        vi.setClickable(true);

        return vi;
    }
}


    public static class OwnMessageHolder {
    public TextView name;
    public TextView message;
}

public static class OtherMessageHolder {
    public TextView name;
    public TextView message;
}

和错误日志:

04-06 23:25:54.720: E/AndroidRuntime(18176): FATAL EXCEPTION: main
04-06 23:25:54.720: E/AndroidRuntime(18176): java.lang.ClassCastException: com.hera.lustrumvirgiel.ChatAdapter$OtherMessageHolder cannot be cast to com.hera.lustrumvirgiel.ChatAdapter$OwnMessageHolder
04-06 23:25:54.720: E/AndroidRuntime(18176):    at com.hera.lustrumvirgiel.ChatAdapter.getView(ChatAdapter.java:57)
04-06 23:25:54.720: E/AndroidRuntime(18176):    at android.widget.AbsListView.obtainView(AbsListView.java:2449)
04-06 23:25:54.720: E/AndroidRuntime(18176):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
04-06 23:25:54.720: E/AndroidRuntime(18176):    at android.widget.ListView.onMeasure(ListView.java:1155)
04-06 23:25:54.720: E/AndroidRuntime(18176):    at android.view.View.measure(View.java:12815)
04-06 23:25:54.720: E/AndroidRuntime(18176):    at android.widget.RelativeLayout.measureChild(RelativeLayout.java:627)
04-06 23:25:54.720: E/AndroidRuntime(18176):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:440)
04-06 23:25:54.720: E/AndroidRuntime(18176):    at android.view.View.measure(View.java:12815)
04-06 23:25:54.720: E/AndroidRuntime(18176):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
04-06 23:25:54.720: E/AndroidRuntime(18176):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
04-06 23:25:54.720: E/AndroidRuntime(18176):    at android.view.View.measure(View.java:12815)
04-06 23:25:54.720: E/AndroidRuntime(18176):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
04-06 23:25:54.720: E/AndroidRuntime(18176):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1399)
04-06 23:25:54.720: E/AndroidRuntime(18176):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:690)
04-06 23:25:54.720: E/AndroidRuntime(18176):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:583)
04-06 23:25:54.720: E/AndroidRuntime(18176):    at android.view.View.measure(View.java:12815)
04-06 23:25:54.720: E/AndroidRuntime(18176):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
04-06 23:25:54.720: E/AndroidRuntime(18176):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
04-06 23:25:54.720: E/AndroidRuntime(18176):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2106)
04-06 23:25:54.720: E/AndroidRuntime(18176):    at android.view.View.measure(View.java:12815)
04-06 23:25:54.720: E/AndroidRuntime(18176):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1068)
04-06 23:25:54.720: E/AndroidRuntime(18176):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2452)
04-06 23:25:54.720: E/AndroidRuntime(18176):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-06 23:25:54.720: E/AndroidRuntime(18176):    at android.os.Looper.loop(Looper.java:137)
04-06 23:25:54.720: E/AndroidRuntime(18176):    at android.app.ActivityThread.main(ActivityThread.java:4493)
04-06 23:25:54.720: E/AndroidRuntime(18176):    at java.lang.reflect.Method.invokeNative(Native Method)
04-06 23:25:54.720: E/AndroidRuntime(18176):    at java.lang.reflect.Method.invoke(Method.java:511)
04-06 23:25:54.720: E/AndroidRuntime(18176):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:788)
04-06 23:25:54.720: E/AndroidRuntime(18176):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
04-06 23:25:54.720: E/AndroidRuntime(18176):    at dalvik.system.NativeStart.main(Native Method)

任何人都知道如何解决这个问题?错误日志引用的行(57)就是这一行:

  

ownMessageHolder =(OwnMessageHolder)vi.getTag();

提前致谢!

2 个答案:

答案 0 :(得分:4)

您正尝试使用多种布局:

vi = inflater.inflate(R.layout.listitem_chat_own, null);
vi = inflater.inflate(R.layout.listitem_chat_other, null);

但是你并没有告诉适配器期望不止一个......这会导致ListView的RecycleBin出现很多问题而且效率不高。只需覆盖getViewTypeCount()getItemViewType(),然后在getItemViewType()内使用getView()加载相应的布局,适配器将负责其余的工作。

答案 1 :(得分:3)

Java的instanceof关键字似乎正是您所寻找的。它允许您确定对象是否是特定类的实例。在转换对象之前,在if语句中使用它特别方便:

Object tag = vi.getTag();
if (tag instanceof OtherMessageHolder)
    otherMessageHolder = (OtherMessageHolder)tag;
else // Assume it is an OwnMessageHolder
    ownMessageHolder = (OwnMessageHolder)tag;