如何在搜索时移除Android SearchView弹出的文本?

15

我已经实现了操作栏SearchView,它正常工作,但是在屏幕底部会弹出一个浮动文本弹出窗口。请参见截图:

screen short

ListView Java类:

@Override
    public boolean onQueryTextChange(String newText) {

        if (TextUtils.isEmpty(newText)) {
            mListView.clearTextFilter();
        } else {

            // EventAdapterView ca = (EventAdapterView)mListView.getAdapter();
            // ca.getFilter().filter(newText.toString());

            // Filter lFilter = mDataAdapter.getFilter();
            // lFilter.filter("");
            // following line was causing the ugly popup window.
            mListView.setFilterText(newText.toString());

            // EventAdapterView ca = (EventAdapterView)mListView.getAdapter();
            // ca.getFilter().filter(newText.toString());

        }

        return true;
    }

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

适配器类

@Override
    public Filter getFilter() {
        /**
         * A filter object which will filter message key
         * */
        Filter filter = new Filter() {

            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint,
                    FilterResults results) {

                mEventUtil = (List<EventUtil>) results.values; // has the
                                                                // filtered
                                                                // values
                notifyDataSetChanged(); // notifies the data with new filtered
                                        // values. Only filtered values will be
                                        // shown on the list
            }

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {

                FilterResults results = new FilterResults(); // Holds the
                                                                // results of a
                                                                // filtering
                                                                // operation for
                                                                // publishing

                List<EventUtil> FilteredArrList = new ArrayList<EventUtil>();

                if (mOriginalValues == null) {
                    mOriginalValues = new ArrayList<EventUtil>(mEventUtil); // mOriginalValues

                }

                if (mListItem == null) {
                    mListItem = new ArrayList<String>();
                    for (EventUtil message : mOriginalValues) {
                        mListItem.add(message.getEvent_Title());
                    }
                }

                /**
                 * 
                 * If constraint(CharSequence that is received) is null returns
                 * the mOriginalValues(Original) values else does the Filtering
                 * and returns FilteredArrList(Filtered)
                 * 
                 **/

                if (constraint == null || constraint.length() == 0) {

                    /*
                     * CONTRACT FOR IMPLEMENTING FILTER : set the Original
                     * values to result which will be returned for publishing
                     */
                    results.count = mOriginalValues.size();
                    results.values = mOriginalValues;
                } else {
                    /* Do the filtering */
                    constraint = constraint.toString().toLowerCase();

                    for (int i = 0; i < mListItem.size(); i++) {
                        String data = mListItem.get(i);
                        if (data.toLowerCase().contains(constraint.toString())) {
                            FilteredArrList.add(mOriginalValues.get(i));
                        }
                    }

                    // set the Filtered result to return
                    results.count = FilteredArrList.size();
                    results.values = FilteredArrList;
                }
                return results;
            }
        };
        return filter;
    }

我该如何移除这个浮动的TextView?


底部显示文本的地方是任何一种视图吗? - Shakeeb Ayaz
如何移除它,可以删除。 - venu
no that is text lable - venu
您可以在Android示例APIDemo中获取完整的源代码-->视图-->搜索视图-->过滤器示例。 - venu
尝试在ListView中覆盖onDisplayHint方法,并且不要调用super实现。 - Leonidos
onDisplayHint可以在Activity或Adapter类中使用。 - venu
2个回答

31

试试这个

首先在你的ListView上禁用TextFilterEnabled

yourListView.setTextFilterEnabled(false);

并像这样过滤您的数据:

android.widget.Filter filter = yourListAdapter.getFilter();


@Override
public boolean onQueryTextChange(String newText) {
    // TODO Auto-generated method stub
    filter.filter(newText);
    return true;
}

就是这样,搜索弹窗顺利消失了。


我尝试了相同的代码,但是不同之处在于你在方法外声明了过滤器对象,而我在方法内部声明了它。我已经进行了更改,现在它完美地工作了...无论如何,感谢@AT_AB。 - venu

2
如果您正在使用CursorLoader,那么应该将搜索查询存储在成员变量中以供以后使用,而不是使用适配器的筛选器:

例如,

private String mSearchQuery;

然后将加载器创建更改为适用于搜索查询

@Override
public Loader<Cursor> onCreateLoader(final int id, final Bundle args) {
    showProgress(true);
    if (TextUtils.isEmpty(mSearchQuery)) {
        return new CursorLoader(
                mActivity,
                CONTENT_URI,
                PROJECTION,
                DEFAULT_SELECTION,
                null,
                SORT_ORDER
        );
    }
    mSearchQuery = "%" + mSearchQuery + "%";
    return new CursorLoader(
            mActivity,
            CONTENT_URI,
            PROJECTION,
            // add the filtering criteria to the default.
            DEFAULT_SELECTION + " AND " + COLUMN_A + " LIKE ?",
            new String[]{mSearchQuery},
            SORT_ORDER
    );
}

最后,确保重新加载加载器。

@Override
public boolean onQueryTextChange(String newText) {
    mSearchQuery = newText;
    // calls the above method
    mActivity.getSupportLoaderManager().
            restartLoader(0, null, this);
    return true;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接