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); 
相关问题