本文介绍了Android的搜索查看犯规进入onQueryTextChange的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图实现与搜索查看福尔摩斯操作栏。我不能够得到查询进入onQueryTextChange方法。不知道怎么回事错了。
公共类MainActivity扩展SherlockFragmentActivity工具
SearchView.OnQueryTextListener {
保护静态的CharSequence [] = _categories {游乐园
鸟类保护区,野生动物,河,山站,天宫}; 受保护的静态布尔[] = _selections新的布尔[_categories.length] 私人最终的String [] =的地方新的String [] {迈索尔,班加罗尔
班加罗尔,瓦亚纳德,本迪布尔国家公园,Chickmaglur
Bandipura,库格,Kodaikanal的,亨比,Ghati Subramanya,
Mekedatu,Muththathhi,Shivasamudram,Talakadu,
Savana杜尔加}; 公共搜索查看mSearchView;
私人TextView的mStatusView; 私人菜单MAINMENU = NULL; PlacesListAdapter适配器;
ListView控件的ListView; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); Log.i(游牧,的onCreate); 清单<&地点GT;想去的地方=新的ArrayList<&地点GT;();
的for(int i = 0; I< places.length;我++){
将PL =新的地方(NO_ID,地方[I],NO_DISTANCE
NO_CATEGORYICON);
thePlaces.add(PL);
} ListView控件=(ListView控件)findViewById(R.id.place_list);
适配器=新PlacesListAdapter(MainActivity.this,R.layout.item_place,
想去的地方); listView.setAdapter(适配器); listView.setOnItemClickListener(新OnItemClickListener(){
@覆盖
公共无效onItemClick(适配器视图<>一种,观景,INT位置,
长ID){ Toast.makeText(MainActivity.this,测试,Toast.LENGTH_SHORT);
startActivity(新意图(MainActivity.this,PlaceActivity.class));
}
}); } @覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
Log.i(游牧,onCreateOptionsMenu);
super.onCreateOptionsMenu(菜单);
MenuInflater吹气= getSupportMenuInflater();
inflater.inflate(R.menu.activity_main,菜单); MAINMENU =菜单; 菜单项searchItem = menu.findItem(R.id.action_search); //搜索查看
mSearchView =(搜索查看)searchItem.getActionView();
setupSearchView(searchItem); mSearchView.setOnSearchClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
//隐藏行动项目
如果(MAINMENU!= NULL){
mainMenu.findItem(R.id.action_category).setVisible(假);
mainMenu.findItem(R.id.action_sort).setVisible(假);
} }
});
mSearchView.setOnCloseListener(新SearchView.OnCloseListener(){
@覆盖
公共布尔的OnClose(){
//重新显示操作按钮
如果(MAINMENU!= NULL){
mainMenu.findItem(R.id.action_category).setVisible(真);
mainMenu.findItem(R.id.action_sort).setVisible(真);
}
返回false; }
}); Log.i(游牧,后setupSearchView());
返回true;
} 公共布尔onOptionsItemSelected(菜单项项){ 开关(item.getItemId()){ 案例R.id.action_search:
Toast.makeText(这一点,Searh,Toast.LENGTH_LONG).show();
Log.i(保荐人,搜索);
打破; 案例R.id.action_category:
的ShowDialog();
打破;
案例R.id.action_sort_alpha_az:
Toast.makeText(这一点,阿尔法AZ,Toast.LENGTH_LONG).show();
打破;
案例R.id.action_sort_alpha_za:
Toast.makeText(这一点,阿尔法ZA,Toast.LENGTH_LONG).show();
打破;
案例R.id.action_sort_dist_nf:
Toast.makeText(这一点,DIST NF,Toast.LENGTH_LONG).show();
打破;
案例R.id.action_sort_dist_fn:
Toast.makeText(这一点,DIST FN,Toast.LENGTH_LONG).show();
打破;
默认:
//返回super.onOptionsItemSelected(项目);
打破;
}
返回true;
} 私人无效setupSearchView(菜单项searchItem){
mSearchView.setIconifiedByDefault(真);
mSearchView.setOnQueryTextListener(本);
mSearchView.setSubmitButtonEnabled(假); searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
// | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
} 公共布尔onQueryTextChange(字符串newText){
Log.i(游牧,onQueryTextChange); 如果(TextUtils.isEmpty(newText)){
Log.i(保荐人,onQueryTextChange空字符串);
listView.clearTextFilter();
}其他{
Log.i(游牧,onQueryTextChange+ newText.toString());
。adapter.getFilter()过滤(newText.toString());
}
返回true;
} 公共布尔onQueryTextSubmit(查询字符串){
Log.i(游牧,onQueryTextSubmit);
返回false;
} 公共布尔的OnClose(){
Log.i(保荐人,的OnClose);
返回false;
} 保护布尔isAlwaysExpanded(){
返回false;
}}
解决方案
添加到您的searchCommand:
searchCommand.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
I am trying to to implement SearchView with Sherlock Action Bar. I am not able to get the query to enter the onQueryTextChange method. Not sure whats going wrong.
public class MainActivity extends SherlockFragmentActivity implements
SearchView.OnQueryTextListener {
protected static CharSequence[] _categories = { "Amusement Park",
"Bird Sanctuary", "Wild Life", "River", "Hill Station", "Temple" };
protected static boolean[] _selections = new boolean[_categories.length];
private final String[] places = new String[] { "Mysore", "Bangalore",
"Mangalore", "Wayanad", "Bandipur National Park", "Chickmaglur",
"Bandipura", "Coorg", "Kodaikanal", "Hampi", "Ghati Subramanya",
"Mekedatu", "Muththathhi", "Shivasamudram", "Talakadu",
"Savana Durga" };
public SearchView mSearchView;
private TextView mStatusView;
private Menu mainMenu = null;
PlacesListAdapter adapter;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("Nomad", "onCreate");
List<Place> thePlaces = new ArrayList<Place>();
for (int i = 0; i < places.length; i++) {
Place pl = new Place("NO_ID", places[i], "NO_DISTANCE",
"NO_CATEGORYICON");
thePlaces.add(pl);
}
listView = (ListView) findViewById(R.id.place_list);
adapter = new PlacesListAdapter(MainActivity.this, R.layout.item_place,
thePlaces);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View view, int position,
long id) {
Toast.makeText(MainActivity.this, "Test", Toast.LENGTH_SHORT);
startActivity(new Intent(MainActivity.this, PlaceActivity.class));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.i("Nomad", "onCreateOptionsMenu");
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
mainMenu = menu;
MenuItem searchItem = menu.findItem(R.id.action_search);
// Search View
mSearchView = (SearchView) searchItem.getActionView();
setupSearchView(searchItem);
mSearchView.setOnSearchClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// hide action item
if (mainMenu != null) {
mainMenu.findItem(R.id.action_category).setVisible(false);
mainMenu.findItem(R.id.action_sort).setVisible(false);
}
}
});
mSearchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
// re-show the action button
if (mainMenu != null) {
mainMenu.findItem(R.id.action_category).setVisible(true);
mainMenu.findItem(R.id.action_sort).setVisible(true);
}
return false;
}
});
Log.i("Nomad", "after setupSearchView()");
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
Toast.makeText(this, "Searh", Toast.LENGTH_LONG).show();
Log.i("Nomad", "Click Search");
break;
case R.id.action_category:
showDialog();
break;
case R.id.action_sort_alpha_az:
Toast.makeText(this, "Alpha AZ", Toast.LENGTH_LONG).show();
break;
case R.id.action_sort_alpha_za:
Toast.makeText(this, "Alpha ZA", Toast.LENGTH_LONG).show();
break;
case R.id.action_sort_dist_nf:
Toast.makeText(this, "Dist NF", Toast.LENGTH_LONG).show();
break;
case R.id.action_sort_dist_fn:
Toast.makeText(this, "Dist FN", Toast.LENGTH_LONG).show();
break;
default:
// return super.onOptionsItemSelected(item);
break;
}
return true;
}
private void setupSearchView(MenuItem searchItem) {
mSearchView.setIconifiedByDefault(true);
mSearchView.setOnQueryTextListener(this);
mSearchView.setSubmitButtonEnabled(false);
searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
// | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
}
public boolean onQueryTextChange(String newText) {
Log.i("Nomad", "onQueryTextChange");
if (TextUtils.isEmpty(newText)) {
Log.i("Nomad", "onQueryTextChange Empty String");
listView.clearTextFilter();
} else {
Log.i("Nomad", "onQueryTextChange " + newText.toString());
adapter.getFilter().filter(newText.toString());
}
return true;
}
public boolean onQueryTextSubmit(String query) {
Log.i("Nomad", "onQueryTextSubmit");
return false;
}
public boolean onClose() {
Log.i("Nomad", "onClose");
return false;
}
protected boolean isAlwaysExpanded() {
return false;
}
}
解决方案
Add this to your searchCommand :
searchCommand.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
这篇关于Android的搜索查看犯规进入onQueryTextChange的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!