我對android很新穎,我有一些過濾的listView和它從橫向模式改變爲縱向模式或反之亦然的問題。我有一個editText用於過濾「drinkSearch」,只要不改變視角(人像與風景),這種過濾就可以工作。這是錯誤,我得到:Android - 嘗試重新打開一個已關閉的對象:使用loaderManager的SQLiteQuery
java.lang.IllegalStateException:嘗試重新打開一個已關閉 對象:SQLiteQuery:選擇_id,名字從飲料
正如你所看到的在下面的代碼中,我使用接口LoaderManager.LoaderCallbacks,這個概念對我來說有點新,我不確定 哪裏出了問題。我會感謝所有幫助,提前致謝!
公共類Drinks_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);
在這一部分,我問我的ContentProvider基於在searchDrink EDITTEXT輸入的字符串一個新的光標。 (下面的代碼,直到「返回查看」只是以下的部分上面,同樣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);
}
}
這裏有兩張照片,以顯示它的外觀的時刻:http://oi42.tinypic.com/dfc702.jpg http://oi43.tinypic.com/2ylqkqa.jpg