All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.venmo.cursor.IterableCursorAdapter Maven / Gradle / Ivy

The newest version!
package com.venmo.cursor;

import android.annotation.TargetApi;
import android.content.Context;
import android.database.Cursor;
import android.os.Build.VERSION_CODES;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;

@TargetApi(VERSION_CODES.HONEYCOMB)
public abstract class IterableCursorAdapter extends CursorAdapter {

    protected IterableCursorAdapter(Context context, IterableCursor c, boolean autoRequery) {
        super(context, c, autoRequery);
    }

    protected IterableCursorAdapter(Context context, IterableCursor c, int flags) {
        super(context, c, flags);
    }

    @Override
    public final View newView(Context context, Cursor cursor, ViewGroup parent) {
        return newView(context, getCursor().peek(), parent);
    }

    public abstract View newView(Context context, T t, ViewGroup parent);

    @Override
    public final void bindView(View view, Context context, Cursor cursor) {
        T t = getCursor().peek();
        bindView(view, context, t);
    }

    public abstract void bindView(View view, Context context, T t);

    @Override
    public IterableCursor runQueryOnBackgroundThread(CharSequence constraint) {
        Cursor cursor = super.runQueryOnBackgroundThread(constraint);
        return enforceIterableCursor(cursor);
    }

    @Override
    public Cursor swapCursor(Cursor newCursor) {
        enforceIterableCursor(newCursor);
        return super.swapCursor(newCursor);
    }

    @Override
    public IterableCursor getCursor() {
        return cast(super.getCursor());
    }

    @Override
    public T getItem(int position) {
        Object superResult = super.getItem(position); // checks validity and moves the cursor
        @SuppressWarnings("unchecked") IterableCursor cursor = ((IterableCursor) superResult);
        return cursor.peek();
    }

    private IterableCursor enforceIterableCursor(Cursor cursor) {
        if (cursor == null) return null;
        if (!(cursor instanceof IterableCursor)) {
            throw new IllegalArgumentException(
                    cursor.getClass().getName() + " is not an " + IterableCursor.class.getName());
        }
        return cast(cursor);
    }

    private IterableCursor cast(Cursor cursor) {
        @SuppressWarnings("unchecked") IterableCursor casted = (IterableCursor) cursor;
        return casted;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy