9

Here它表示不建議使用SimpleCursorAdapter的API級別1的構造函數,並且建議使用LoaderManagerCursorLoaderSimpleCursorAdapter的舊構造函數已棄用..真的嗎?

但是鑽研LoaderManagerCursorLoader的使用我發現this例如延伸一個ListFragment(一個片段本身的延伸我想),我們創建一個CursorLoader內部類內何處。除了CursorLoaderUri作爲參數外,一切看起來都很好。所以這意味着我需要創建ContentProvider才能訪問我的數據庫。

我必須承認它看起來像一個矯枉過正的必須通過所有這些只是爲了創建一個簡單的ListView來自數據庫的項目。特別是,如果我無意將我的數據庫數據提供給其他應用程序,並且內容提供商的主要目的是要這樣做。

那麼它真的值得嗎?

特別是在像我這樣的情況下,要獲取的內容可能會很小。我認真考慮以舊的方式來做,你說什麼?

+1

你是否支持API 11或更高版本? – Cristian

+0

不,當然我不是,我願意使用兼容性庫,使以前版本支持碎片和裝載機。 – Bilthon

+0

你發現的樣品名稱是什麼(看起來像我想在我的應用程序中做的事)?鏈接只是解釋一般的示例.. – Karl

回答

8

我寫了一個simple CursorLoader並不需要內容提供商:

import android.content.Context; 
import android.database.Cursor; 
import android.support.v4.content.AsyncTaskLoader; 

/** 
* Used to write apps that run on platforms prior to Android 3.0. When running 
* on Android 3.0 or above, this implementation is still used; it does not try 
* to switch to the framework's implementation. See the framework SDK 
* documentation for a class overview. 
* 
* This was based on the CursorLoader class 
*/ 
public abstract class SimpleCursorLoader extends AsyncTaskLoader<Cursor> { 
    private Cursor mCursor; 

    public SimpleCursorLoader(Context context) { 
     super(context); 
    } 

    /* Runs on a worker thread */ 
    @Override 
    public abstract Cursor loadInBackground(); 

    /* Runs on the UI thread */ 
    @Override 
    public void deliverResult(Cursor cursor) { 
     if (isReset()) { 
      // An async query came in while the loader is stopped 
      if (cursor != null) { 
       cursor.close(); 
      } 
      return; 
     } 
     Cursor oldCursor = mCursor; 
     mCursor = cursor; 

     if (isStarted()) { 
      super.deliverResult(cursor); 
     } 

     if (oldCursor != null && oldCursor != cursor && !oldCursor.isClosed()) { 
      oldCursor.close(); 
     } 
    } 

    /** 
    * Starts an asynchronous load of the contacts list data. When the result is ready the callbacks 
    * will be called on the UI thread. If a previous load has been completed and is still valid 
    * the result may be passed to the callbacks immediately. 
    * <p/> 
    * Must be called from the UI thread 
    */ 
    @Override 
    protected void onStartLoading() { 
     if (mCursor != null) { 
      deliverResult(mCursor); 
     } 
     if (takeContentChanged() || mCursor == null) { 
      forceLoad(); 
     } 
    } 

    /** 
    * Must be called from the UI thread 
    */ 
    @Override 
    protected void onStopLoading() { 
     // Attempt to cancel the current load task if possible. 
     cancelLoad(); 
    } 

    @Override 
    public void onCanceled(Cursor cursor) { 
     if (cursor != null && !cursor.isClosed()) { 
      cursor.close(); 
     } 
    } 

    @Override 
    protected void onReset() { 
     super.onReset(); 

     // Ensure the loader is stopped 
     onStopLoading(); 

     if (mCursor != null && !mCursor.isClosed()) { 
      mCursor.close(); 
     } 
     mCursor = null; 
    } 
} 

只需要AsyncTaskLoader類。無論是在Android 3中的一個。0或更高版本,或兼容性軟件包附帶的軟件包。

+0

非常感謝@Cristian – confucius

+0

爲SimpleCursorLoader找到了一個不錯的代碼示例 - https://bitbucket.org/ssutee/418496_mobileapp/src/fc5ee705a2fd/demo/DotDotListDB/src/th/ac/ku/android/sutee/dotdotlist - found它非常有用! – Shushu

4

只需使用它下面的構造函數,即獲取標誌的構造函數。不要使用FLAG_AUTO_REQUERY,只需將0傳遞給標誌。

除非您真的需要在用戶查看ListView時處理對底層數據庫的數據更改,那麼您無需擔心需要重新查詢。

另一方面,如果您希望ListView在用戶查看列表時顯示數據庫的更改,請按照Google的建議並使用CursorLoader。

編輯:

由於第二構造僅僅是API,你可能只是想延長自己的CursorAdapter 11個可用。你幾乎只需要實現bindView和newView,你就完成了。

1

僅使用simpleCursorAdapter不推薦使用的構造函數。 我開發我的應用程序時出現了這種錯誤,但我使用它,它與我的應用程序完美結合。或者嘗試在android開發人員網站中使用下面的不贊成使用的構造函數,該構造函數有一個額外的參數,即帶有它的標誌參數。

+0

但請注意,第二個構造函數僅在api級別11中引入。因此,我必須添加兼容性庫才能使用它。添加一個靜態庫也使得我的apk更大,如果僅僅是爲了使用這個構造函數,我真的沒有看到它的重點。 – Bilthon

1

我相信CursorLoader目前打算用於ContentProvider。

如果您希望使用新框架從數據庫直接加載;您可以考慮擴展AsyncTaskLoader並從onCreateLoader返回而不是使用CursorLoader。

如果您使用現有的方法,您必須更加小心您的查詢操作需要多長時間。如果您的查詢需要花費大量的時間,請考慮使用AsyncTask加載遊標(並瞭解在UI線程中運行的重新查詢)。

0

我知道這個線程是舊的,但你可以在SimpleCursorAdapter對象創建中添加最後一個參數。只需添加「,0」。

這是Android喜歡的標誌,警告消失。

例子:

SimpleCursorAdapter dataAdapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.item_list_layout, cursor, fromDB(), toLayout(), 0); 
相關問題