我有一个ArrayList,其中包含从SQLite数据库中获取的数据。 ArrayList通过ArrayAdapter.it添加到ListView。问题是当我使用SearchView过滤列表时。它不起作用。 在这里我的代码:
public class ListActivity extends Activity implements ActionBar.OnNavigationListener {
List<InforTask> list =new ArrayList<InforTask>();
ListAdapter adapter;
ListView lv;
Menu mnOption = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
list = dbHelp.getAllInfor();
adapter = new ListAdapter(ListActivity.this,R.layout.view_list_item,list);
lv = (ListView) findViewById(R.id.contact_list);
lv.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.list, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
mnOption = menu;
mSearchView = (SearchView) searchItem.getActionView();
mSearchView
.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText.toString());
return false;
}
});
return true;
}
} //
<item
android:id="@+id/action_search"
android:actionViewClass="android.widget.SearchView"
android:showAsAction="always"
android:title="Search..."/>