一种用在搜索查看变换所述ArrayAdapter到的CursorAdapter [英] Converting an ArrayAdapter to CursorAdapter for use in a SearchView
问题描述
我如何可以转换 ArrayAdapter<字符串>
的静态数据成的CursorAdapter
的使用建议监听器搜索查看
?
我已经构建了 ArrayAdapter<字符串>
从静态数据(<$ C C $> allString )
ArrayAdapter&LT;字符串&GT; searchAdapter =新的ArrayAdapter&LT;字符串&GT;(背景下,R.layout.listitem,allString);
和我用它为 MultiAutoCompleteTextView
这与API级设备工作正常,小于11
MultiAutoCompleteTextView findTextView.setAdapter(searchAdapter);
不过我的目标API的级别是11和API> 10我使用动作条
中,我想有一个搜索查看代替。
下面是我曾尝试:它确实表明了动作条
与嵌入式搜索查看
,但没有给出任何建议,因为它会在 MultiAutoCompleteTextView
。
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
MenuInflater充气= getMenuInflater();
如果(android.os.Build.VERSION.SDK_INT→10){
inflater.inflate(R.menu.menu11,菜单);
搜索查看=(搜索查看)menu.findItem(R.id.MENU_SEARCH).getActionView();
INT []为= {0};
CursorAdapter的CursorAdapter的=新SimpleCursorAdapter(背景下,R.layout.listitem,空,allBusStopString,到);
searchView.setSuggestionsAdapter(CursorAdapter的);
searchView.setOnSuggestionListener(新OnSuggestionListener(){
@覆盖
公共布尔onSuggestionClick(INT位置){
字符串将selectedItem =(字符串)cursorAdapter.getItem(位置);
Log.v(搜索视图,则selectedItem);
返回false;
}
@覆盖
公共布尔onSuggestionSelect(INT位置){
返回false;
}
});
}其他{
inflater.inflate(R.menu.menu,菜单);
}
返回true;
}
这是奇怪的 SearchView.setSuggestionsAdapter
接受的CursorAdapter而已。
您可以创建 MatrixCursor 并从String数组的数据填充它。我希望你有小的数据集合。
然后将光标移动到CursorAdapter的。
的String [] columnNames = {_id,文本}
MatrixCursor光标=新MatrixCursor(columnNames);
。String []数组= getResources()getStringArray(R.array.allStrings); //如果字符串的资源
的String []临时=新的String [2];
INT标识= 0;
对于(字符串项:数组){
温度[0] = Integer.toString(ID ++);
温度[1] =项目;
cursor.addRow(临时);
}
从字符串= {文本} [];
INT []到= {R.id.name_entry};
busStopCursorAdapter =新SimpleCursorAdapter(背景下,R.layout.listentry,光标,从,到);
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> 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 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.
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
.
@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;
}
That's strange SearchView.setSuggestionsAdapter
accepts CursorAdapter only.
You could create MatrixCursor and fill it with data from String array. I hope you have small data collection.
Then pass the cursor to 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);
这篇关于一种用在搜索查看变换所述ArrayAdapter到的CursorAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!