title: Cast class to its subclass in Kotlin
body: <p>I have a list of a class called Opportunity, which is filled with objects that extend Opportunity. The list can have either CommunityOpportunites or SponsorshipOpportunities.</p>
  
  <p>I override getItemViewType and assign a value to each item based on whichever subclass the object in the relevant postition, and have a different ViewHolder for each:</p>
  
  <pre><code>override fun getItemViewType(position: Int): Int {
  
      return if (opportunityList[position] is SponsorshipOpportunity){
  
          Log.i(TAG,"Item type is sponsorshipId")
  
          SPONSORSHIP
  
      } else{
          Log.i(TAG,"Item type is community")
          COMMUNITY
  
      }
  
  }
  
      inner class CommunityViewHolder(var view: CommunityTileBinding):RecyclerView.ViewHolder(view.root)
      inner class SponsorshipViewHolder(var view: SponsorshipTileBinding):RecyclerView.ViewHolder(view.root)
  
  
  
  companion object{
      private const val COMMUNITY = 0
      private const val SPONSORSHIP = 1
  }
  </code></pre>
  
  <p>In onCreateViewHolder() I create the proper ViewHolder for the class of the item, and in onBindViewHolder() I attempt to cast the items in the list (of type Opportunity in the constructor) to the subclass of the item in the view:</p>
  
  <pre><code>override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
  
  
      val inflater = LayoutInflater.from(parent.context)
  
      return when (viewType){
  
          COMMUNITY-&gt;CommunityViewHolder(DataBindingUtil.inflate(inflater, R.layout.community_tile, parent, false))
          SPONSORSHIP-&gt; SponsorshipViewHolder(DataBindingUtil.inflate(inflater, R.layout.sponsorship_tile, parent, false))
  
          else-&gt; throw IllegalArgumentException()
  
  
      }
  }
  
  override fun getItemCount(): Int = opportunityList.size
  
  
  override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
  
    when(holder.itemViewType){
        COMMUNITY-&gt; {
  
            (holder as CommunityViewHolder).view.communityOpportunity = opportunityList[position] as CommunityOpportunity
        }
        SPONSORSHIP-&gt;{
            (holder as SponsorshipViewHolder).view.sponsorship = opportunityList[position] as SponsorshipOpportunity
            holder.view.postActionText.text = context.resources.getString(R.string.watch_respond)
  
        }
    }
  
  }
  </code></pre>
  
  <p>However, I get the following class cast exception</p>
  
  <pre><code>java.lang.ClassCastException: com.example.model.Opportunity cannot be cast to com.example.model.CommunityOpportunity
  </code></pre>
  
  <p>when I try at the relevant line in onBindViewHolder, even though the log statement confirming that the item is a CommunityOpportunity in getItemViewType() is printed.</p>
  
  <p>Is there a better way to ask, or is there a better way for me to be displaying multiple ViewHolder/Object types in the RecyclerView?</p>
  
  <p>Edit: Here are the relevant xml layouts:</p>
  
  
  
  <p>
  
  <pre><code>xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"&gt;
  
  &lt;data&gt;
      &lt;variable
          name="sponsorship"
          type="com.example.weare8sample.model.SponsorshipOpportunity"/&gt;
  &lt;/data&gt;
  
  &lt;LinearLayout
  
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_margin="10dp"
      android:elevation="2dp"
      android:orientation="vertical"
      android:background="@drawable/tile_background"&gt;
  
      &lt;ImageView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:foreground="@drawable/tile_background"
          android:imageUrl="@{sponsorship.coverTileUri}"&gt;
  
      &lt;/ImageView&gt;
  
  
  
      &lt;RelativeLayout
          android:layout_width="match_parent"
          android:layout_height="50dp"
          android:gravity="center_vertical"
          android:padding="10dp"&gt;
  
          &lt;TextView
              android:id="@+id/postActionText"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:fontFamily="@font/lato_bold"
              tools:text="@string/watch_respond"&gt;
  
          &lt;/TextView&gt;
  
          &lt;ImageView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentRight="true"
              android:src="@drawable/ic_chevron_right_black_36dp"&gt;
  
          &lt;/ImageView&gt;
  
  
  
      &lt;/RelativeLayout&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p></p>
  
  
  
  <p>
  
  <pre><code>xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"&gt;
  
  &lt;data&gt;
  
      &lt;variable
          name="communityOpportunity"
          type="com.example.weare8sample.model.CommunityOpportunity" /&gt;
  &lt;/data&gt;
  
  
  &lt;ImageView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_margin="10dp"
      android:background="@drawable/tile_background"
      android:imageUrl="@{communityOpportunity.mediaImageUri}"&gt;
  
  &lt;/ImageView&gt;
  </code></pre>
  
  <p></p>