我对android还很陌生,过滤后的listView存在一些问题,它从横向模式变为纵向模式或反之亦然。我有一个用于过滤“drinkSearch”的editText,只要我不改变视角(纵向与横向),此过滤就可以工作。这是我得到的错误:
java.lang.IllegalStateException:尝试重新打开一个已经关闭的对象:SQLiteQuery:SELECT _id,name FROM Drinks
正如您在下面的代码中看到的那样,我使用了接口 LoaderManager.LoaderCallbacks,这个概念对我来说有点新,我不确定哪里出了问题。我将感谢所有帮助,在此先感谢!
公共类 Drinks_Fragment 扩展 Fragment 实现 LoaderManager.LoaderCallbacks {
private static final int DRINKS_LIST_LOADER = 0x01;
private SimpleCursorAdapter adapter;
private ListView drinksList;
private String LOG;
private EditText drinkSearch;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.drinks_list, container, false);
drinkSearch = (EditText)view.findViewById(R.id.drinkInputSearch);
drinksList = (ListView) view.findViewById(R.id.drinksList);
drinksList.setEmptyView(view.findViewById(R.id.empty_list_view));
String[] from = {DrinksTable.COLUMN_NAME};
int[] to = {R.id.drinkName};
getLoaderManager().initLoader(DRINKS_LIST_LOADER, null, this);
adapter = new SimpleCursorAdapter(getActivity().getApplicationContext(), R.layout.drinks_list_item,null, from, to, 0);
drinksList.setAdapter(adapter);
在这一部分中,我根据在 searchDrink 编辑文本中输入的字符串向我的 contentProvider 请求一个新的光标。(以下代码,直到“返回视图”就在上面部分的下方,相同的 onCreateView 方法)
drinkSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
// When user changed the Text
adapter.getFilter().filter(s.toString());
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
adapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
String value = "%"+constraint.toString()+"%";
ContentResolver content = getActivity().getContentResolver();
return content.query(CupProvider.DRINKS_URI,new String[]{DrinksTable.COLUMN_ID,DrinksTable.COLUMN_NAME},DrinksTable.COLUMN_NAME + " LIKE ?",new String[]{value},null);
}
});
return view;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu,v,menuInfo);
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.drink_actions,menu);
}
@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
String[] projection = {DrinksTable.COLUMN_ID, DrinksTable.COLUMN_NAME};
CursorLoader cursorLoader = new CursorLoader(getActivity(), CupProvider.DRINKS_URI, projection, null, null, null);
return cursorLoader;
}
@Override
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
adapter.swapCursor(cursor);
}
@Override
public void onLoaderReset(Loader<Cursor> cursorLoader) {
// data is not available anymore, delete reference
adapter.swapCursor(null);
}
}
这里有 2 张图片来展示它目前的样子:http://oi42.tinypic.com/dfc702.jpg http://oi43.tinypic.com/2ylqkqa.jpg