在官方LoaderManager示例过滤...为什么MySearchView?

时间:2013-06-03 11:44:48

标签: android

我正在研究LoaderManager周围的事情,我试图在我自己的应用程序中修改类似的功能。我发现了一些我不清楚的细节。可能是代码从更大的东西中提取出来的情况,细节是示例中不必要的额外功能。也可能是我忽略原因......

首先,示例介绍

public static class MySearchView extends SearchView...

仅覆盖onActionViewCollapsed以清除查询。是什么原因?

其次mCurFilter初始化为null,并针对nullonQueryTextChange中的onCreateLoader进行了测试。不是空字符串更好,更强大,并且导致比null指示更简单的实现?具体而言,onQueryTextChange实现为:

public boolean onQueryTextChange(String newText) {
    // Called when the action bar search text has changed.  Update
    // the search filter, and restart the loader to do a new query
    // with this filter.
    String newFilter = !TextUtils.isEmpty(newText) ? newText : null;

    // Don't do anything if the filter hasn't actually changed.
    // Prevents restarting the loader when restoring state.
    if (mCurFilter == null && newFilter == null) {
        return true;
    }
    if (mCurFilter != null && mCurFilter.equals(newFilter)) {
        return true;
    }
    mCurFilter = newFilter;
    getLoaderManager().restartLoader(0, null, this);
    return true;
}

为什么将newText转换为newFilter(对于空字符串为null),然后将复杂的比较转换为null非-null值完成。难道它不能简单地实现为folows(保留的原始注释)?

public boolean onQueryTextChange(String newText) {
    // Called when the action bar search text has changed.  Update
    // the search filter, and restart the loader to do a new query
    // with this filter.

    // Don't do anything if the filter hasn't actually changed.
    // Prevents restarting the loader when restoring state.
    if (mCurFilter.equals(newText)) {
        return true;
    }
    mCurFilter = newText;
    getLoaderManager().restartLoader(0, null, this);
    return true;
}

onCreateLoader的{​​{1}} couuld测试,而不是mCurFilter.isEmpty()

感谢您的时间和经验。

0 个答案:

没有答案