Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typo in getItem(int position) for FlowCursorList.java #9

Closed
jparkie opened this issue Nov 25, 2014 · 1 comment
Closed

Typo in getItem(int position) for FlowCursorList.java #9

jparkie opened this issue Nov 25, 2014 · 1 comment

Comments

@jparkie
Copy link

jparkie commented Nov 25, 2014

In the following method, it retrieves a Model from the Cursor for this List. However, the call "SqlUtils.convertToModel(false, mTable, mCursor);" should be "SqlUtils.convertToModel(true, mTable, mCursor);"

FlowCursorList.java

    /**
     * Returns a model at the specified position. If we are using the cache and it does not contain a model
     * at that position, we move the cursor to the specified position and construct the {@link ModelClass}.
     *
     * @param position The row number in the {@link android.database.Cursor} to look at
     * @return The {@link ModelClass} converted from the cursor
     */
    public ModelClass getItem(int position) {
        ModelClass model;
        if (cacheModels) {
            model = mModelCache.get(position);
            if (model == null && mCursor.moveToPosition(position)) {
                model = SqlUtils.convertToModel(false, mTable, mCursor);
                mModelCache.put(position, model);
            }
        } else {
            mCursor.moveToPosition(position);
            model = SqlUtils.convertToModel(false, mTable, mCursor);
        }
        return model;
    }

This should be the following:

    /**
     * Returns a model at the specified position. If we are using the cache and it does not contain a model
     * at that position, we move the cursor to the specified position and construct the {@link ModelClass}.
     *
     * @param position The row number in the {@link android.database.Cursor} to look at
     * @return The {@link ModelClass} converted from the cursor
     */
    public ModelClass getItem(int position) {
        ModelClass model;
        if (cacheModels) {
            model = mModelCache.get(position);
            if (model == null && mCursor.moveToPosition(position)) {
                model = SqlUtils.convertToModel(true, mTable, mCursor);
                mModelCache.put(position, model);
            }
        } else {
            mCursor.moveToPosition(position);
            model = SqlUtils.convertToModel(true, mTable, mCursor);
        }
        return model;
    }

Because, in SqlUtils.java, the convertToModel(boolean isList, Class table, Cursor cursor) method resets the Cursor to the first position if isList is false. Accordingly, this typo in FlowCursorList causes getItem(int position) to not work properly as any movement to a position specified for the Cursor is reset.

SqlUtils.java

/**
     * Takes first {@link ModelClass} from the cursor
     *
     * @param isList       If it's a list, do not reset the cursor
     * @param table        The model class that we convert the cursor data into.
     * @param cursor       The cursor from the DB
     * @param <ModelClass>
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <ModelClass extends Model> ModelClass convertToModel(boolean isList, Class<ModelClass> table, Cursor cursor) {
        ModelClass model = null;
        try {
            if (isList || cursor.moveToFirst()) {
                ModelAdapter<ModelClass> modelAdapter = FlowManager.getModelAdapter(table);
                if(modelAdapter == null) {
                    Class persistentClass = (Class) ((ParameterizedType) table.getGenericSuperclass()).getActualTypeArguments()[0];
                    if(persistentClass.isAssignableFrom(BaseModelView.class)) {
                        model = (ModelClass) FlowManager.getModelViewAdapter((Class<? extends BaseModelView<? extends Model>>)table).loadFromCursor(cursor);
                    }
                } else {
                    model = modelAdapter.loadFromCursor(cursor);
                }
            }
        } catch (Exception e) {
            FlowLog.log(FlowLog.Level.E, "Failed to process cursor.", e);
        }

        return model;
    }
@agrosner
Copy link
Owner

@jparkie good find! will be in 1.0.2

agrosner pushed a commit that referenced this issue Nov 25, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants