title: ListAdapter with multiple item types throws ClassCastException
body: <p>I am building an app for an event and one of the features is a message board. To do this I built a <code>ListView</code> and loaded it with messages. </p>
  
  <p>I have two types of message: messages you send, and messages the other send. The layout is slightly different, U use other XML files. I tried my code(below) and it works fine if all the items are of the same type (yours, or the others). But when the types are changing, I get a <code>ClassCastException</code>. I know what that means, and where it goes wrong (the itemholders are of different classes, and it tries to make an object of the class its not) but not how to solve it. </p>
  
  <p>Here is my code, this is the getView of my <code>ChatAdapter</code>. 
  <code>ChatAdapter</code> is a class that extends <code>BaseAdapter</code>. </p>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p>and the error log:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Anyone knows how to solve this problem? The line the error log is referring to (57) is this one:  </p>
  
  <blockquote>
    <p>ownMessageHolder = (OwnMessageHolder)vi.getTag();</p>
  </blockquote>
  
  <p>Thanks in advance!</p>