在搜索查看转换的ArrayAdapter来的CursorAdapter使用(Converting a

2019-06-26 12:03发布

我怎么能转换成一个ArrayAdapter<String>静态数据为CursorAdapter在使用建议监听器SearchView ? 我已经构建了ArrayAdapter<String>从静态数据( allString

ArrayAdapter<String> searchAdapter = new ArrayAdapter<String>(context, R.layout.listitem, allString);

我用它为MultiAutoCompleteTextView这与设备API级工作正常,小于11

MultiAutoCompleteTextView findTextView.setAdapter(searchAdapter);

但是我的目标是API级别是11和API> 10我用一个ActionBar内,我想有一个搜索查看代替。

这是我曾尝试:它确实表明了ActionBar与嵌入式SearchView ,但没有给出任何建议,因为它会在MultiAutoCompleteTextView

@Override
public boolean onCreateOptionsMenu(Menu menu) {     
        MenuInflater inflater = getMenuInflater();

         if (android.os.Build.VERSION.SDK_INT > 10){
             inflater.inflate(R.menu.menu11, menu);
             searchView = (SearchView) menu.findItem(R.id.MENU_SEARCH).getActionView();
             int[] to = {0};
             CursorAdapter cursorAdapter = new SimpleCursorAdapter(context, R.layout.listitem, null, allBusStopString, to);
             searchView.setSuggestionsAdapter(cursorAdapter);
             searchView.setOnSuggestionListener(new OnSuggestionListener() {

                @Override
                public boolean onSuggestionClick(int position) {
                    String selectedItem = (String)cursorAdapter.getItem(position);
                    Log.v("search view", selectedItem);
                    return false;
                }

                @Override
                public boolean onSuggestionSelect(int position) {
                    return false;
                }
             });  
         }else{
             inflater.inflate(R.menu.menu, menu);
         }

    return true;
}

Answer 1:

真奇怪SearchView.setSuggestionsAdapter只接受的CursorAdapter。

你可以创建MatrixCursor并从String数组的数据填充它。 我希望你有小数据集合。

然后将光标传递给CursorAdapter的。

    String[] columnNames = {"_id","text"}
    MatrixCursor cursor = new MatrixCursor(columnNames);
    String[] array = getResources().getStringArray(R.array.allStrings); //if strings are in resources
    String[] temp = new String[2];
    int id = 0;
    for(String item : array){
        temp[0] = Integer.toString(id++);
            temp[1] = item;
        cursor.addRow(temp);
    }               
    String[] from = {"text"}; 
    int[] to = {R.id.name_entry};
    busStopCursorAdapter = new SimpleCursorAdapter(context, R.layout.listentry, cursor, from, to);


Answer 2:

我正面临类似的问题。 您可以使用SearchView.setSuggestionsAdapter()只接受的CursorAdapter。 在另一方面......这有什么意义? 如果使用标准<android.support.v7.widget.SearchView />那么它包含SearchAutoComplete其内部延伸AppCompatAutoCompleteTextView。 下面的代码为我工作:

List<String> items = Lists.newArrayList(new String[] {"aaaaa", "bbbbb", "ccccc", "ddddd"});
SearchView searchView = (SearchView) findViewById(R.id.autocomplete_searchview);
SearchView.SearchAutoComplete searchSrcTextView = (SearchView.SearchAutoComplete) findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchSrcTextView.setThreshold(1);
searchSrcTextView.setAdapter(new SuggestionAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, items));
searchSrcTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        return;
    }
});

和下面的适配器代码:

public class SuggestionAdapter<T> extends ArrayAdapter<T> {

    private List<T> items;
    private List<T> filteredItems;
    private ArrayFilter mFilter;

    public SuggestionAdapter(Context context, @LayoutRes int resource, @NonNull List<T> objects) {
        super(context, resource, Lists.<T>newArrayList());
        this.items = objects;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public T getItem(int position) {
        return items.get(position);
    }

    @Override
    public Filter getFilter() {
        if (mFilter == null) {
            mFilter = new ArrayFilter();
        }
        return mFilter;
    }

    public int getCount() {
        //todo: change to pattern-size
        return items.size();
    }

    private class ArrayFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence prefix) {
            FilterResults results = new FilterResults();

            //custom-filtering of results
            results.values = items;
            results.count = items.size();

            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            filteredItems = (List<T>) results.values;
            if (results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }
    }
}


文章来源: Converting an ArrayAdapter to CursorAdapter for use in a SearchView