Android搜索框

时间:2016-10-12 23:54:53

标签: android android-actionbar logcat

将搜索框留在操作栏上时,通过选择其中一个列表视图项,或选择后退按钮返回活动,我会收到以下错误。

W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection
W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection

搜索功能正在按预期工作,但日志中充满了错误消息。

我查看了所有类似的问题,但不幸的是,所有问题都涉及edittext元素的问题。如果我能弄清楚如何引用系统搜索小部件,我会在onPause事件中使用此代码。

InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(???myEditText???.getWindowToken(), 0);

主要活动的相关代码:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    MenuItem searchMenuItem = menu.findItem(R.id.search);
    SearchView searchView = (SearchView) searchMenuItem.getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setSubmitButtonEnabled(true);
    searchView.setOnQueryTextListener(this);

    return true;
}

@Override
public boolean onQueryTextSubmit(String query) {
    return false;
}

@Override
public boolean onQueryTextChange(String newText) {
    familyAdapter.getFilter().filter(newText);

    return true;
}

适配器代码:

@NonNull
@Override
public Filter getFilter() {
    if (familyFilter == null) {
        familyFilter = new FamilyFilter();
    }
    return familyFilter;
}

private class FamilyFilter extends Filter {

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults filterResults = new FilterResults();
        if (constraint != null && constraint.length() > 0) {
            ArrayList<Family> tempList = new ArrayList<Family>();
            for (Family family : mFamilyList) {
                if (family.getLocalName().toLowerCase().contains(constraint.toString().toLowerCase())) {
                    tempList.add(family);
                }
            }
            filterResults.count = tempList.size();
            filterResults.values = tempList;
        } else {
            filterResults.count = mFamilyList.size();
            filterResults.values = mFamilyList;
        }

        return filterResults;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        mFilteredList = (ArrayList<Family>) results.values;
        notifyDataSetChanged();
    }
}

我很难过下一步该做什么。

0 个答案:

没有答案