package net.ichigotake.common.widget;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class CursorAdapter<I, T> extends android.support.v4.widget.CursorAdapter {

    public static interface CursorCreator<I> {

        I create(Cursor cursor);
    }

    private final ViewBinder<I, T> binder;
    private final LayoutInflater inflater;
    private final CursorCreator<I> creator;

    public CursorAdapter(CursorAdapterParameter<I, T> parameter) {
        this(
                parameter.getContext(),
                parameter.getCursor(),
                parameter.getViewBinder(),
                parameter.getCursorCreator()
        );
    }

    public CursorAdapter(Context context, Cursor c, ViewBinder<I, T> binder, CursorCreator<I> creator) {
        super(context, c, false);
        this.binder = binder;
        this.inflater = LayoutInflater.from(context);
        this.creator = creator;
    }

    @Override
    public I getItem(int position) {
        Cursor cursor = (Cursor)super.getItem(position);
        return cursor != null ? creator.create(cursor) : null;
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        int position = cursor.getPosition();
        I item = creator.create(cursor);
        View newView = binder.generateView(position, item, inflater, parent);
        newView.setTag(binder.generateTag(position, item, newView));
        return newView;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        T tag = (T)view.getTag();
        I item = creator.create(cursor);
        int position = cursor.getPosition();
        binder.bindView(position, item, tag);
    }

}

@@ -380,24 +380,36 @@
 e final 
-ViewBind
+CursorAdapterParamet
 er%3CI, T%3E
@@ -409,20 +409,23 @@
 r%3CI, T%3E 
-bind
+paramet
 er;%0A    
@@ -466,52 +466,8 @@
 ter;
-%0A    private final CursorCreator%3CI%3E creator;
 %0A%0A  
@@ -543,30 +543,14 @@
     
-this(%0A                
+super(
 para
@@ -568,32 +568,16 @@
 ntext(),
-%0A               
  paramet
@@ -587,24 +587,32 @@
 getCursor(),
+ false);
 %0A           
@@ -612,42 +612,68 @@
     
-        parameter.getViewBinder(),
+this.inflater = LayoutInflater.from(parameter.getContext());
 %0A   
@@ -677,24 +677,21 @@
         
-        
+this.
 paramete
@@ -695,37 +695,20 @@
 eter
-.getCursorCreator()%0A        )
+ = parameter
 ;%0A  
@@ -713,29 +713,32 @@
     %7D%0A%0A    p
-ublic
+rotected
  CursorAdapt
@@ -743,236 +743,64 @@
 pter
-(Context context, Cursor c, ViewBinder%3CI, T%3E binder, CursorCreator%3CI%3E creator) %7B%0A        super(context, c, false);%0A        this.binder = binder;%0A        this.inflater = LayoutInflater.from(context);%0A        this.creator = creato
+Parameter%3CI, T%3E getParameter() %7B%0A        return paramete
 r;%0A 
@@ -945,23 +945,44 @@
  null ? 
-c
+parameter.getCursorC
 reator
+()
 .create(
@@ -1148,39 +1148,60 @@
        I item = 
-c
+parameter.getCursorC
 reator
+()
 .create(cursor);
@@ -1224,22 +1224,41 @@
 wView = 
-b
+parameter.getViewB
 inder
+()
 .generat
@@ -1321,22 +1321,41 @@
 .setTag(
-b
+parameter.getViewB
 inder
+()
 .generat
@@ -1556,15 +1556,36 @@
 m = 
-c
+parameter.getCursorC
 reator
+()
 .cre
@@ -1654,14 +1654,33 @@
     
-b
+parameter.getViewB
 inder
+()
 .bin

add CursorAdapter#getParameter