title: Convert custom adapter to use generics in Java
body: <p>I have a Adapter in my Android app and I want to make it Generic.</p>
  
  <p>Basiclly the Adapter looks like that:</p>
  
  <pre><code>public class myAdapter extends FragmentStatePagerAdapter {
  
  DataTypaA myFragment;
  DataTypeB data;
  DataTypeC items;
  
  public myAdapter(FragmentManager fm, DataTypaA fragment) {
          data = new SparseArray&lt;DataTypeB&gt;();
          myFragment = fragment;
          items = myFragment.getData();
      }
  
      public DataTypeB getItem(int position) {
          return data.get(position);
      }
  
      @Override
      public int getCount() {
          return items.getList().size();
      }
  
  
  public void setData () {
  
  items = myFragment.getItems ()  //getItems return DataTypeC
  data.setTheData (items) 
  }
  }
  </code></pre>
  
  <p>I changed it to generic </p>
  
  <pre><code>public class myAdapter &lt;A,B,C&gt; extends FragmentStatePagerAdapter {
  
  A myFragment;
  B data;
  C items;
  
  public myAdapter(FragmentManager fm, A fragment) {
          data = new SparseArray&lt;B&gt;();
          myFragment = fragment;
          items =  (C) myFragment.getData();
      }
  
      public B getItem(int position) {
          return data.get(position);
      }
  
      @Override
      public int getCount() {
          return items.getList().size();
      }
  
  
  public void setData () {
  
  items = myFragment.getItems ()  //getItems return DataTypeC
  data.setTheData (items) 
  }
  }
  </code></pre>
  
  <p>but I'm getting different errors, when methods should get <code>DataTypeC</code> parameter and I passing it parameter type <code>C</code> ( <code>data.setTheData (items)</code>  ) that is actually type <code>DataTypeC</code> the compiler suggest to cast <code>C</code> to type <code>DataTypeC</code>. and in <code>getCount()</code> I have also error suggesting to convert <code>items</code> to <code>DataTypeC</code>.</p>
  
  <p>for example when I try to override getItem I getting mistakes, but when I create the same method with other name it compiles.</p>
  
  <pre><code>//Error - "The return type is incompatible with FragmentStatePagerAdapter.getItem(int)"
  @Override
      public B getItem(int position) {
          return (B) data.get(position);
      }
  
  //compiles
      public B getItemTest(int position) {
          return data.get(position);
      }
  </code></pre>
  
  <p>Any ideas how to fix it so it will be 100% generic?</p>
  
  <p><strong>ADDED</strong>:     After your answer I changes it to support return of generic type : </p>
  
  <pre><code>   public class TypeA &lt;T&gt; {
      private T mData;
  
          public T getData() { return mData;; }
      }
  
  
  
      public class myAdapter &lt;A,B,C&gt; extends FragmentStatePagerAdapter {
  
          A myFragment;
          B data;
          C items;
  
          public myAdapter(FragmentManager fm, A fragment) {
                  data = new SparseArray&lt;B&gt;();
                  myFragment = fragment;
                  items = myFragment.getData(); //Error - The method getData() is undefined for the type T
              }
  
  }
  </code></pre>
  
  <p>I'm getting compile error...
  when I run it of course  <code>&lt;T&gt;</code> and <code>&lt;C&gt;</code> are the same type. </p>