2011-12-15 28 views
2

我有一個類,我想過濾一個列表使用編輯文本,但我得到一個StaleDataException每當編輯文本被帶回到空白,所以用戶鍵入數據和刪除直到editText爲空,並且這是異常被觸發的地方。所以這是我的代碼。我得到一個StaleDataException

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

爲什麼你使用cur.requery(); startManagingCursor(cur)之後;在下面的代碼中Cursor cur = data.query(); startManagingCursor(CUR); cur.requery(); – jennifer 2011-12-15 10:40:06

+0

我已刪除重新查詢,我仍然收到此錯誤。 – aspiringCoder 2011-12-15 10:54:23

回答

1

您試圖從已關閉的光標檢索信息。您必須驗證光標是否關閉/而不是通過使用isClosed方法。

Cursor cur = data.query(); 
     if (TextUtils.isEmpty((_constraint)){ 

     if (cur != null && cur .moveToFirst()) { 
     return cur ; 

    } 
} 

cur.close();