Android 将 ArrayAdapter 转换为 CursorAdapter 以在 SearchView 中使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11628172/
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
Converting an ArrayAdapter to CursorAdapter for use in a SearchView
提问by Behzad Momahed Heravi
How can I convert an ArrayAdapter<String>
of static data into a CursorAdapter
for using Suggestion Listener in SearchView
?
I have constructed the ArrayAdapter<String>
from static data (allString
)
如何将 的ArrayAdapter<String>
静态数据转换为CursorAdapter
用于在 中使用建议侦听器的SearchView
?我已经ArrayAdapter<String>
从静态数据 ( allString
)
ArrayAdapter<String> searchAdapter = new ArrayAdapter<String>(context, R.layout.listitem, allString);
and I use it for an MultiAutoCompleteTextView
which works fine in devices with API level less than 11
我将它用于MultiAutoCompleteTextView
在 API 级别低于 11 的设备中正常工作的
MultiAutoCompleteTextView findTextView.setAdapter(searchAdapter);
However my target API is level is 11 and for API>10 I use an ActionBar
within which I would like to have a SearchView instead.
然而,我的目标 API 是 11 级,对于 API>10,我使用了一个ActionBar
,我希望在其中使用SearchView。
Here's what I have tried: It does show the ActionBar
with the embedded SearchView
but does not give any suggestions as it would in the MultiAutoCompleteTextView
.
这是我尝试过的:它确实显示了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;
}
回答by pawelzieba
That's strange SearchView.setSuggestionsAdapter
accepts CursorAdapter only.
奇怪的是SearchView.setSuggestionsAdapter
只接受 CursorAdapter。
You could create MatrixCursorand fill it with data from String array. I hope you have small data collection.
您可以创建MatrixCursor并用字符串数组中的数据填充它。我希望你有小数据收集。
Then pass the cursor to CursorAdapter.
然后将光标传递给 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);
回答by kosiara - Bartosz Kosarzycki
I was facing similar problem. You can use SearchView.setSuggestionsAdapter()which only accepts CursorAdapter. On the other hand... what's the point? If you use the standard <android.support.v7.widget.SearchView />
then it contains SearchAutoComplete which extends AppCompatAutoCompleteTextView inside. The following code worked for me:
我面临着类似的问题。您可以使用仅接受CursorAdapter 的SearchView.setSuggestionsAdapter()。另一方面……有什么意义?如果您使用该标准,那么它包含 SearchAutoComplete,它在内部扩展 AppCompatAutoCompleteTextView。以下代码对我有用:<android.support.v7.widget.SearchView />
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;
}
});
and the following Adapter code:
以及以下适配器代码:
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();
}
}
}
}