Android - ActionBar SearchView建议使用简单的String数组

时间:2014-05-14 15:09:04

标签: android android-actionbar ormlite searchview

我想实现一个具有建议能力的ActionBar搜索小工具 我已经存储在我的ORMLite数据库中的字符串数组,希望用于建议 如何在不创建类加载的情况下执行此操作(Provider,Searchable ......!)?

3 个答案:

答案 0 :(得分:49)

这是一个样本,我希望它可以帮到你。

要在此游标中实现更多功能,例如。按下建议项目时打开活动,请查看此http://developer.android.com/guide/topics/search/adding-custom-suggestions.html

 package com.roberto.android.thetracker.app;

    import android.database.MatrixCursor;
    import android.os.Bundle;
    import android.provider.BaseColumns;
    import android.support.v4.app.Fragment;
    import android.support.v4.view.MenuItemCompat;
    import android.support.v4.widget.CursorAdapter;
    import android.support.v4.widget.SimpleCursorAdapter;
    import android.support.v7.widget.SearchView;
    import android.view.Menu;
    import android.view.MenuInflater;

    public class CustomSearchFragment extends Fragment {

        private static final String[] SUGGESTIONS = {
                "Bauru", "Sao Paulo", "Rio de Janeiro",
                "Bahia", "Mato Grosso", "Minas Gerais",
                "Tocantins", "Rio Grande do Sul"
        };
        private SimpleCursorAdapter mAdapter;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            final String[] from = new String[] {"cityName"};
            final int[] to = new int[] {android.R.id.text1};
            mAdapter = new SimpleCursorAdapter(getActivity(),
                    android.R.layout.simple_list_item_1,
                    null,
                    from,
                    to,
                    CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            setHasOptionsMenu(true);
        }

        @Override
        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
            super.onCreateOptionsMenu(menu, inflater);
            inflater.inflate(R.menu.main, menu);
        }

        @Override
        public void onPrepareOptionsMenu(Menu menu) {
            super.onPrepareOptionsMenu(menu);
            SearchView searchView = (SearchView) MenuItemCompat
                    .getActionView(menu.findItem(R.id.action_search));
            searchView.setSuggestionsAdapter(mAdapter);
            searchView.setIconifiedByDefault(false);
            // Getting selected (clicked) item suggestion
            searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
                @Override
                public boolean onSuggestionClick(int position) {

                    Cursor cursor = (Cursor) mAdapter.getItem(position);
        String txt = cursor.getString(cursor.getColumnIndex("cityName"));
        searchview.setQuery(txt, true);
                    return true;
                }

                @Override
                public boolean onSuggestionSelect(int position) {
                    // Your code here
                    return true;
                }             
            });
            searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String s) {
                    return false;
                }

                @Override
                public boolean onQueryTextChange(String s) {
                    populateAdapter(s);
                    return false;
                }
            });
        }

        // You must implements your logic to get data using OrmLite
        private void populateAdapter(String query) {
            final MatrixCursor c = new MatrixCursor(new String[]{ BaseColumns._ID, "cityName" });
            for (int i=0; i<SUGGESTIONS.length; i++) {
                if (SUGGESTIONS[i].toLowerCase().startsWith(query.toLowerCase()))
                    c.addRow(new Object[] {i, SUGGESTIONS[i]});
            }
            mAdapter.changeCursor(c);
        }

    }

答案 1 :(得分:3)

基于betorcs的回答

你不需要

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {...});

你可以使用它,而不是那个

mAdapter.setFilterQueryProvider(new FilterQueryProvider() {
    @Override
    public Cursor runQuery(CharSequence constraint) {
        return populateAdapter((String) constraint);
    }
});

并在populateAdapter上,替换&#34;返回c;&#34;使用&#34; mAdapter.changeCursor(c);&#34;

private Cursor populateAdapter(String query) {
    .
    .
    .
    return c;
}

答案 2 :(得分:1)

 private String getSuggestion(int position) {
        MatrixCursor cursor = (MatrixCursor) searchView.getSuggestionsAdapter().getCursor();
        String suggest1 = cursor.getString(position);


        return SUGGESTIONS[Integer.valueOf(suggest1)];
    }