我想我不正确地理解如何使用CursorTreeAdapter.setChildrenCursor(),因为我得到了StackOverflowError,因为在LogCat中看起来像是一个循环。

循环中的模式是:


我的getChildrenCursor()实现重新启动了Loader
CursorTreeAdapter.setChildrenCursor()调用CursorTreeAdapter.getChildrenCursorHelper(),其中
回到我的实现getChildrenCursor


我怀疑Android的源代码可能表现出不正确的行为(请参见最终代码段)。



这是我的代码中的相关方法:

CursorTreeAdapter

        /**
        * Return null, but set off a Loader that will set the Cursor after asynchronous loading
        **/
        @Override
        protected Cursor getChildrenCursor(Cursor groupCursor) {
                getLoaderManager().initLoader(groupCursor.getInt(0), null, CollectionsMasterFragment.this);
                return null;
        }


LoaderCallbacks

@Override
public void onLoadFinished(android.support.v4.content.Loader loader, Cursor cursor) {
    d("onLoadFinished with "
            + cursor.getCount() + " elements");
    // Set children cursor to correct group
    switch (loader.getId()) {
        case POS_0:
            mCollectionsAdapter.setChildrenCursor(POS_0, cursor);
            break;
        case POS_1:
            mCollectionsAdapter.setChildrenCursor(POS_1, cursor);
            break;
        default:
            throw new IllegalArgumentException(
                    "Could not handle loader id " + loader.getId());
    }
}

@Override
public android.support.v4.content.Loader<Cursor> onCreateLoader(int id, Bundle bundle) {
    switch (id) {
        case POS_0:
            d("Creating new dossiers loader");
            return new Model.Dossier.Loader(getActivity());
        case POS_1:
            d("Creating new bundles loader");
            return new Model.Bundle.Loader(getActivity());
        default:
            throw new IllegalArgumentException(
                    "Could not handle loader id " + id);
    }
}




这是CursorTreeAdapter的Android 4.4代码:

 /**
 * Sets the children Cursor for a particular group. If there is an existing cursor
 * it will be closed.
 * <p>
 * This is useful when asynchronously querying to prevent blocking the UI.
 *
 * @param groupPosition The group whose children are being set via this Cursor.
 * @param childrenCursor The Cursor that contains the children of the group.
 */
public void setChildrenCursor(int groupPosition, Cursor childrenCursor) {

    /*
     * Don't request a cursor from the subclass, instead we will be setting
     * the cursor ourselves.
     */
    MyCursorHelper childrenCursorHelper = getChildrenCursorHelper(groupPosition, false);

    /*
     * Don't release any cursor since we know exactly what data is changing
     * (this cursor, which is still valid).
     */
    childrenCursorHelper.changeCursor(childrenCursor, false);
}

/**
 * Gets the cursor helper for the children in the given group.
 *
 * @param groupPosition The group whose children will be returned
 * @param requestCursor Whether to request a Cursor via
 *            {@link #getChildrenCursor(Cursor)} (true), or to assume a call
 *            to {@link #setChildrenCursor(int, Cursor)} will happen shortly
 *            (false).
 * @return The cursor helper for the children of the given group
 */
synchronized MyCursorHelper getChildrenCursorHelper(int groupPosition, boolean requestCursor) {
    MyCursorHelper cursorHelper = mChildrenCursorHelpers.get(groupPosition);

    if (cursorHelper == null) {
        if (mGroupCursorHelper.moveTo(groupPosition) == null) return null;

        final Cursor cursor = getChildrenCursor(mGroupCursorHelper.getCursor());
        cursorHelper = new MyCursorHelper(cursor);
        mChildrenCursorHelpers.put(groupPosition, cursorHelper);
    }

    return cursorHelper;
}


如您所见,requestCursor的参数getChildrenCursorHelper被忽略,因此getChildrenCursor再次被调用!

这是怎么回事?这是错误吗? setChildrenCursor不应该只是执行childrenCursorHelper = new MyCursorHelper(childrenCursor),而不是向适配器询问childrenCursor吗?

最佳答案

我也在努力从加载器中填充可扩展列表视图,但是我认为您的问题的一部分与事实有关,您必须为每种类型或模式实现多个加载器,请查看此链接是否有帮助* Set multiple cursor loaders with multiple adapters - Android *

关于android - CursorTreeAdapter.setChildrenCursor()导致循环,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20910277/

10-12 04:31