4

我嘗試使用數據庫表的數據填充建議列表。但我得到StaleDataException s。它非常隨機地拋出,但總是當我在textview中輸入一個字符時。使用CursorLoader和SimpleCursorAdapter的AutoCompleteTextView

這裏是我的代碼:

CursorLoader延長CristianSimpleCursorLoader

public class TagCursorLoader extends SimpleCursorLoader { 

    private String mSelection; 
    private TagDbLoader mDbLoader; 

    public TagCursorLoader(Context context, TagDbLoader dBLoader, String selection) { 
     super(context); 
     this.mDbLoader = dBLoader; 
     this.mSelection = selection; 
    } 

    @Override 
    public Cursor loadInBackground() { 
     return mDbLoader.fetchContainingString(mSelection); 
    } 

} 

的裝載機回調:

public class TagCursorLoaderCallback implements LoaderCallbacks<Cursor>, CursorToStringConverter { 

    private Context mContext; 
    private TagDbLoader mdDbLoader; 
    private SimpleCursorAdapter mAdapter; 
    private String mSelection; 

    public TagCursorLoaderCallback(Context context, TagDbLoader dBLoader, SimpleCursorAdapter adapter) { 
     this.mContext = context; 
     this.mdDbLoader = dBLoader; 
     mAdapter = adapter; 
     mSelection = ""; 
    } 

    @Override 
    public Loader<Cursor> onCreateLoader(int id, Bundle args) { 
     return new TagCursorLoader(mContext, mdDbLoader, mSelection); 
    } 

    @Override 
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) { 

     if (!data.isClosed()) { 
      mAdapter.swapCursor(data); 
     } 
    } 

    @Override 
    public void onLoaderReset(Loader<Cursor> loader) { 
     mAdapter.swapCursor(null); 
    } 

    public void setSelection(String mSelection) { 
     this.mSelection = mSelection; 
    } 

    @Override 
    public CharSequence convertToString(Cursor cursor) { 
     return cursor.getString(cursor.getColumnIndexOrThrow(DbConstants.Tags.KEY_TAG)); 
    } 

} 

最後當我成立了AutoCompleteTextView

private void initializeAutoComplete() { 

     mTagDbLoader = new TagDbLoader(getActivity()); 
     mTagDbLoader.open(); 

     mTagInput = (AutoCompleteTextView) mLayout.findViewById(R.id.autoComplete); 

     mTagInput.addTextChangedListener(new TextWatcherAdapter() { 

      @Override 
      public void afterTextChanged(Editable s) { 
       mLoaderCallback.setSelection(s.toString()); 
       getLoaderManager().restartLoader(0, null, mLoaderCallback); 
      } 
     }); 

     mAdapter = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_list_item_1, 
       null, new String[] { DbConstants.Tags.KEY_TAG }, new int[] { android.R.id.text1 }, 
       0); 

     mLoaderCallback = new TagCursorLoaderCallback(getActivity(), mTagDbLoader, mAdapter); 
     mAdapter.setCursorToStringConverter(mLoaderCallback); 
     mTagInput.setAdapter(mAdapter); 
     getLoaderManager().initLoader(0, null, mLoaderCallback); 
    } 

回答

6

經過一番調查,看起來繼承自ResourceCursorAdapter,它繼承自CursorAdapterCursorAdapter使用CursorFilter進行過濾,並且此類在其publishResults()中調用changeCursor()changeCursor關閉舊遊標...所以這就是爲什麼我的遊標自動關閉。

我放棄了裝載機,並改變了執行下面的代碼,它的工作原理很大:

mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, 
       mTagDbLoader.fetchAll(), new String[] { DbConstants.Tags.KEY_TAG }, 
       new int[] { android.R.id.text1 }, 0); 

mAdapter.setFilterQueryProvider(new FilterQueryProvider() { 

    @Override 
    public Cursor runQuery(CharSequence constraint) { 

     if (constraint == null || constraint.equals("")) 
      return mAdapter.getCursor(); 

     return mTagDbLoader.fetchContainingString(constraint.toString()); 
    } 
}); 

mAdapter.setCursorToStringConverter(new CursorToStringConverter() { 

    @Override 
    public CharSequence convertToString(Cursor c) { 

     return c.getString(c.getColumnIndexOrThrow(DbConstants.Tags.KEY_TAG)); 
    } 
}); 
+0

你的帖子的幫助下,通過不通過它的內容,由我需要重寫convertToString方法的信息!, 謝謝 –

相關問題