Android - 带有简单字符串数组的 ActionBar SearchView 建议

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23658567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:20:26  来源:igfitidea点击:

Android - ActionBar SearchView suggestions with a simple String array

androidandroid-actionbarormlitesearchview

提问by Mehdiway

I want to implement an ActionBar Search Widget with suggestions ability.
I already havea string array stored in my ORMLite database that I want to use for the suggestions.
How can I do this without creating loads of classes (Provider, Searchable...!)?

我想实现一个具有建议功能的 ActionBar 搜索小部件。
我已经在我的 ORMLite 数据库中存储了一个字符串数组,我想将其用于建议。
如何在不创建大量类(提供程序、可搜索...!)的情况下执行此操作?

回答by betorcs

It's a sample, I hope it can help you.

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

To implements more features in this cursor, eg. open activity when press in suggestion item, take a look on this http://developer.android.com/guide/topics/search/adding-custom-suggestions.html

要在此游标中实现更多功能,例如。在建议项中按下时打开活动,看看这个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);
         final 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);
     }

 }

回答by Ali_dev

Base on betorcs's answer :

基于betorcs的回答

you don't need to have

你不需要

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

you can use this, instead of that

你可以用这个,而不是那个

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

and on populateAdapter, Replace "return c;" with "mAdapter.changeCursor(c);"

并在 populateAdapter 上,替换“return c;” 使用“mAdapter.changeCursor(c);”

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

回答by Malti Patel

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


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