title: A cleaner way of "transforming" Cursors in Pojo(s)
body: <p>I do a lot of apps that use SQLite and very often cannot design the database, because I have to use the Schema defined by the customer.</p>
  
  <p>Obviously :-P the UX/UI designer does not care about the underlying data...she/he has to be cool!</p>
  
  <p>So I, not infrequently, have to use a lot of <code>INNER JOIN</code>, <code>LEFT JOIN</code> with very big SQL queries (but thanks to the power of ContentProviders and Services this is not a big problem).</p>
  
  <p>What bores me is the "cut/paste" (or static final) code for handling the different Cursor(s) from all the adapters <code>.getItem(position)</code>. </p>
  
  <p>So I was thinking of a way to keep things more clean, something like this.</p>
  
  <h3>CursorHandler.java</h3>
  
  <p>Implementations of this interface will convert a Cursor into an object.
  <strong><code>T</code></strong> is the target type the input Cursor will be converted to.</p>
  
  <pre><code>public interface CursorHandler&lt;T&gt; {
     T handle(Cursor cu)
  }
  </code></pre>
  
  <h3>CursorHandlerAdapter.java</h3>
  
  <p>Create a custom CursorAdapter that will use the above interface to automatically convert a cursor to a specified pojo</p>
  
  <pre><code>abstract
  public class CursorHandlerAdapter&lt;T&gt; extends CursorAdapter {
  
     private LayoutInflater    mInflater  = null;
     private CursorHandler&lt;T&gt;  mHandler   = null;
  
     public CursorHandlerAdapter(Context context, CursorHandler&lt;T&gt; handler) {
       super(context, null, 0);
       mHandler  = handler;
       mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     }
  
     @Override
     public T getItem(int position) {
       Cursor cu = (Cursor)super.getItem(position);
       return ((cu == null) ? null : mHandler.handle(cu));
     }
  
  
     protected T getItem(Cursor cu) {
       return mHandler.handle(cu);
     }
  
     protected LayoutInflater getLayoutInflater() { return mInflater; }   
  };
  </code></pre>
  
  <h3>Usage example</h3>
  
  <p>Suppose we have a pojo, let's call it <em>Item</em>:</p>
  
  <pre><code>public class Item  implements Parcelable {
  
     public long     id;
     public String   code;
     public String   title;
     public String   description;
     public String   imageUrl;
  
     public Item() {  }
  
     @Override
     public String toString() {
        StringBuilder sb = new StringBuilder("[");
        sb.append("id=");
        sb.append(this.id);
        sb.append(", ");
        /* append the others fields */
        return sb.toString();
     }
  
     @Override
     public boolean equals(Object obj) {
        if (obj == null) return false;
        if (obj == this) return true;
        if (!(obj instanceof Item)) return false;
  
        Item test = (Item)obj;
  
        boolean result = true;
        result = result &amp;&amp; (this.code == test.code || 
           (this.code != null &amp;&amp; this.code.equals(test.code)));
        return result;
     }
  
     @Override
     public int hashCode() {
        int result = 8;
        result = 31 * result + (this.code == null ? 0 : this.code.hashCode());
        return result;    
     }
  
     @Override
     public void writeToParcel(Parcel parcel, int flags) {
        parcel.writeLong(this.id);
        parcel.writeString(this.code);
        /* write the others fields */
     }
  
     static final
     public Creator&lt;Item&gt; CREATOR = new Creator&lt;Item&gt;() {
        @Override
        public Item createFromParcel(Parcel source) {
           return new Item(source);
        }
  
        @Override
        public Item[] newArray(int size) {
           return new Item[size];
        }
     };
  
     private Item(Parcel in) {
        this.id = in.readLong();
        this.code = in.readString();
        /* read the others fields */
     }
  };
  </code></pre>
  
  <p>Let's create our CursorHandler implementation:</p>
  
  <pre><code>public class ItemCursorHandler implements CursorHandler&lt;Item&gt; {
  
     @Override
     public Item handle(Cursor cu) throws SQLException {
        Item item = new Item();
  
        int idx = cu.getColumnIndex(CommonsColumns.ID);
        if (idx != -1)
           item.id = cu.getLong(idx);
  
        idx = cu.getColumnIndex(ItemColumns.CODE);
        if (idx != -1)
           item.code = cu.getString(idx);
  
        idx = cu.getColumnIndex(ItemColumns.TITLE);
        if (idx != -1)
           item.title = cu.getString(idx);
  
        idx = cu.getColumnIndex(ItemColumns.DESCRIPTION);
        if (idx != -1)
           item.description = cu.getString(idx);
  
        idx = cu.getColumnIndex(ItemColumns.IMAGE_URL);
        if (idx != -1)
           item.imageUrl = cu.getString(idx);
  
        return item;
     }
  }
  </code></pre>
  
  <p>and, then, the related CursorAdapter:</p>
  
  <pre><code>public class ItemsCursorAdapter extends CursorHandlerAdapter&lt;Item&gt; {
  
     public ItemsCursorAdapter(Context context) {
        super(context, new ItemCursorHandler());
     }
  
     @Override
     public void bindView(View view, Context context, Cursor cursor) {
       /* Here we can fetch our pojo */ 
       Item item = getItem(cursor);
  
       /* Get the ViewHolder as usual and fill/set the views */
       ViewHolder holder = (ViewHolder) view.getTag();
       holder.xxxx.setYYY(....); 
     }
  
     @Override
     public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View rowView = getLayoutInflater().inflate(R.layout.my_very_cool_layout, parent, false);
  
        ViewHolder holder = new ViewHolder();
        holder.xxxx = (YYYY)rowView.findViewById(R.id.yyyy);
        ...
        ...
        rowView.setTag(holder);
  
        return rowView;
     }
  
     static
     private class ViewHolder {
        YYYY  xxxx;
        ...
        ...
     };
  }
  </code></pre>
  
  <p>In ours Fragments or Activities we can retrieve the pojo:</p>
  
  <pre><code>ItemsCursorAdapter adapter = ...
  Item value = adapter.getItem(position);
  /* do whatever with the pojo */
  </code></pre>
  
  <p>In this way I don't need to "cut/copy or singletonize" all the different cursorToPojo methods.</p>
  
  <p>Is this a proper way of handling the Cursor ? or there are some possible drawbacks ?</p>
  
  <p>Could you point me to more consolidated alternatives ?</p>
  
  <p>Best Regards,
  LuS</p>