2

我正在研究 LoaderManager 周围的东西,我正在尝试以建议的方式在我自己的应用程序中修复类似的功能。我发现了一些我不清楚的细节。可能是代码是从更大的东西中提取的,而细节是示例中不必要的额外功能。也可能是我忽略了原因...

首先,实例介绍

public static class MySearchView extends SearchView...

覆盖onActionViewCollapsed唯一清除查询。是什么原因?

其次mCurFilter被初始化null并针对nullinonQueryTextChange和进行测试onCreateLoadernull与for 指示相比,空字符串不是更好、更健壮并且实现更简单吗?具体来说,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转换为newFilternull对于空字符串),然后完成对null非空值的复杂比较。它不能简单地实现如下(保留原始评论)吗?

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可以测试而mCurFilter.isEmpty()不是mCurFilter == null.

感谢您的时间和经验。

4

0 回答 0