Android SearchView小部件工作不正常
我试图使用searchview acitonbar小部件过滤我的listview,但当我在edittext中键入内容时,屏幕上会出现一个黑框。如何在没有黑盒子的情况下进行搜索。 这是我的密码Android SearchView小部件工作不正常,android,listview,android-widget,searchview,Android,Listview,Android Widget,Searchview,我试图使用searchview acitonbar小部件过滤我的listview,但当我在edittext中键入内容时,屏幕上会出现一个黑框。如何在没有黑盒子的情况下进行搜索。 这是我的密码 public class MainActivity extends ListActivity implements OnQueryTextListener{ private Button favListBtn; private ListView mListView; @Ove
public class MainActivity extends ListActivity implements OnQueryTextListener{
private Button favListBtn;
private ListView mListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] categories = {"Food", "Cloth", "Shoes","Agriculture",
"Food", "Cloth", "Shoes","Agriculture",
"Food", "Cloth", "Shoes","Agriculture",
"Food", "Cloth", "Shoes","Agriculture",
"Food", "Cloth", "Shoes","Agriculture",
"Food", "Cloth", "Shoes","Agriculture",
"Food", "Cloth", "Shoes","Agriculture"};
mListView = (ListView) findViewById(android.R.id.list);
mListView = getListView();
ArrayAdapter<String> mAdapte = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, categories);
mListView.setAdapter(mAdapte);
mListView.setTextFilterEnabled(true);
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
new SearchAsynTask(MainActivity.this).execute();
// selected item
String category = ((TextView) view).getText().toString();
Intent intent = new Intent(MainActivity.this, BusinessListActivity.class);
startActivity(intent);
Toast.makeText(MainActivity.this, category, Toast.LENGTH_LONG).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
SearchManager searchManager = (SearchManager) getSystemService( Context.SEARCH_SERVICE );
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setSubmitButtonEnabled(true);
searchView.setOnQueryTextListener(this);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String query) {
if (TextUtils.isEmpty(query))
{
mListView.clearTextFilter();
}
else
{
mListView.setFilterText(query.toString());
}
return true;
}
}
显示该黑框是因为您设置了列表视图:mListView.setTextFilterEnabledtrue 编辑: 你说过没有上面这行你就无法进行搜索。这是因为您正在使用文本过滤进行搜索。这不是一个好办法。您正在调用mListView.setFilterTextquery.toString;在onQueryTextChange回调中。因此,如果不启用文本过滤,您的搜索机制将无法工作 正确的方法是使您的ListView适配器实现接口并在那里处理过滤
然后你应该搜索对象:Filter.performFilteringCharSequence@Tim我更新了我的问题。我想这是因为你是Toast.makeTextMainActivity.this,category,Toast.LENGTH\u LONG.show;没有这一行,我无法在我的listview中搜索。