我有一个类,我想使用编辑文本过滤列表,但是每当编辑文本返回空白时我都会收到StaleDataException,因此用户键入数据并将其删除,直到editText为空这就是触发异常的地方。所以这是我的代码。
public class CBFilter extends ListActivity {
EditText Filter;
ListView RecipeNames;
Cursor cursor;
CBListAdapter adapter;
CBDataBaseHelper data;
SQLiteDatabase data2;
TextView RecipeText, RowId;
String[] from = { CBDataBaseHelper.KEY_NAME};
int[] to = { R.id.rowText};
ImageView image;
byte[] dataImage;
BufferedInputStream buf;
public static final String TAG = "Error";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//FileInputStream in;//= openFileInput("//STEFAN-PC/Users/stefan/Desktop/Uni Work/Image.jpg");
RecipeNames = (ListView) findViewById(android.R.id.list);
RecipeNames.setTextFilterEnabled(true);
RecipeText = (TextView) findViewById(R.id.recipeText);
Filter = (EditText) findViewById(R.id.search_box);
//adapter = new SimpleCursorAdapter (this, 0, cursor, null, null);
//image = (ImageView) findViewById(R.id.RecipeImage);
data = new CBDataBaseHelper(this);
data.open();
cursor = data.query();
startManagingCursor(cursor);
adapter = new CBListAdapter(this, 0, cursor, from, to);
RecipeNames.setAdapter(adapter);
adapter.notifyDataSetChanged();
Filter.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
try{
CBListAdapter filteradapter = (CBListAdapter)RecipeNames.getAdapter();
filteradapter.setFilterQueryProvider(filterQueryProvider);
//RecipeNames.setAdapter(filteradapter);
filteradapter.getFilter().filter(s);
filteradapter.notifyDataSetChanged();
}catch(Exception e){
Log.e(TAG, "sumthing wrong", e);
e.printStackTrace();
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void afterTextChanged(Editable s) {
}
});
}
public void CreateNew(View view){
Intent myIntent = new Intent(this, CBCreate.class);
startActivity(myIntent);
}
@Override
public void onListItemClick(ListView parent, View v, int position, long id) {
super.onListItemClick(parent, v, position, id);
Intent intent1 = new Intent(this, CBCreate.class);
long rowId = cursor.getLong(cursor.getColumnIndex(CBDataBaseHelper.KEY_ROWID));
String s = String.valueOf(rowId);
intent1.putExtra("SELECTED", s);
startActivity(intent1);
}
public FilterQueryProvider filterQueryProvider = new FilterQueryProvider() {
public Cursor runQuery(CharSequence _constraint) {
// CBDataBaseHelper dh2;
Cursor c = data.findRecipe((String) _constraint);
startManagingCursor(c);
Cursor cur = data.query();
startManagingCursor(cur);
cur.requery();
if (_constraint == null | _constraint.length() == 0) {
return cur;
}
return c;
}
};
}
这就是我在logCat中显示的内容。
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): android.database.StaleDataException: Access closed cursor
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:217)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at pkg.CookBook.CBListAdapter.getView(CBListAdapter.java:41)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.widget.AbsListView.obtainView(AbsListView.java:1294)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.widget.ListView.makeAndAddView(ListView.java:1727)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.widget.ListView.fillDown(ListView.java:652)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.widget.ListView.fillFromTop(ListView.java:709)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.widget.ListView.layoutChildren(ListView.java:1580)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.widget.AbsListView.onLayout(AbsListView.java:1147)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.view.View.layout(View.java:7034)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.view.View.layout(View.java:7034)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.view.View.layout(View.java:7034)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.view.View.layout(View.java:7034)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.view.View.layout (View.java:7034)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.view.ViewRoot.performTraversals(ViewRoot.java:1049)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.view.ViewRoot.handleMessage(ViewRoot.java:1744)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.os.Handler.dispatchMessage(Handler.java:99)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.os.Looper.loop(Looper.java:143)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.app.ActivityThread.main(ActivityThread.java:4914)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at java.lang.reflect.Method.invokeNative(Native Method)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at java.lang.reflect.Method.invoke(Method.java:521)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:1)
您正在尝试从已关闭的Cursor中检索信息。您必须使用isClosed方法验证光标是否已关闭。
Cursor cur = data.query();
if (TextUtils.isEmpty((_constraint )){
if (cur != null && cur .moveToFirst()) {
return cur ;
}
}
cur.close();