8

我正在尝试使用 LoaderCallBacks 从数据库中加载数据。但是不会调用 loadercallbacks 中的 onLoadFinished 函数。因此,正如您在日志中看到的那样,返回的行数为 1,但列表未显示任何条目,也没有调用 onLoadFinished,如您所见。

系列片段

    公共类 SerieFragment 扩展了 SherlockListFragment 实现
            LoaderCallbacks {

        私有 SimpleCursorAdapter 适配器;

        @覆盖
        公共无效 onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            String[] from = new String[] { DBContract.Episodes.NAME };
            int[] to = new int[] { R.id.name };

            Log.i("LoaderManager", "正在启动...");
            getLoaderManager().initLoader(0, null, this);

            适配器 = 新的 SimpleCursorAdapter(getActivity(),
                    R.layout.serie_list_item,空,从,到,0);

            this.setListAdapter(适配器);
        }

        @覆盖
        public void onViewCreated(View view, Bundle savedInstanceState) {

        }

        @覆盖
        公共加载器 onCreateLoader(int id, Bundle args) {
            Log.i("LoaderManager", "正在创建加载器...");

            字符串 [] 投影 = { DBContract.Episodes._ID,
                    DBContract.Episodes.NAME };
            CursorLoader cursorLoader = new CursorLoader(this.getActivity(),
                    Uri.parse(SerieProvider.CONTENT_URI + "/episodes"), 投影,
                    空,空,空);
            返回游标加载器;
        }

        @覆盖
        public void onLoadFinished(Loader loader, Cursor cursor) {
            Log.i("LoaderManager",
                    "完成加载条目... - 光标:" + cursor.getCount());
            适配器.swapCursor(光标);
        }

        @覆盖
        公共无效onLoaderReset(加载器加载器){
            Log.i("LoaderManager", "正在重置加载器...");
            适配器.swapCursor(null);
        }

内容提供商

    公共类 SerieProvider 扩展 ContentProvider {

        私有静态 DatabaseHelper 数据库;
        private static final String PROVIDER = "com.drizzlyday.apps.episotron.providers";
        私有静态最终字符串 BASE_PATH = "serieprovider";
        公共静态最终 Uri CONTENT_URI = Uri.parse("content://" + PROVIDER
                + "/" + BASE_PATH);

        公共静态最终 int 系列 = 1;
        公共静态最终 int EPISODES = 2;

        私有静态最终 UriMatcher uriMatcher;
        静止的 {
            uriMatcher = 新的 UriMatcher(UriMatcher.NO_MATCH);
            uriMatcher.addURI(PROVIDER, BASE_PATH, SERIES);
            uriMatcher.addURI(PROVIDER, BASE_PATH + "/episodes", EPISODES);
        }

        @覆盖
        公共布尔 onCreate() {
            数据库 = 新的 DatabaseHelper(getContext());
            返回假;
        }

        @覆盖
        公共 int 删除(Uri uri,字符串选择,字符串 [] selectionArgs){
            // TODO 自动生成的方法存根
            返回0;
        }

        @覆盖
        公共字符串getType(Uri uri){
            // TODO 自动生成的方法存根
            返回空值;
        }

        @覆盖
        公共 Uri 插入(Uri uri,ContentValues 值){
            // TODO 自动生成的方法存根
            返回空值;
        }

        @覆盖
        公共游标查询(Uri uri,String[] 投影,字符串选择,
                String[] selectionArgs, String sortOrder) {

            Log.d("提供者", uri.toString());
            SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();

            int uriType = uriMatcher.match(uri);
            开关(uriType){
            案例系列:
                Log.d("提供者", "系列");
                queryBuilder.setTables(DBContract.Series.TABLE_NAME);
                休息;
            案例情节:
                Log.d("提供者", "剧集");
                queryBuilder.setTables(DBContract.Episodes.TABLE_NAME);
                休息;
            默认:
                throw new IllegalArgumentException("未知 URI:" + uri);
            }

            SQLiteDatabase db = database.getReadableDatabase();
            SQLiteCursor cursor = (SQLiteCursor) queryBuilder.query(db, projection,
                    选择, selectionArgs, null, null, sortOrder);
            cursor.setNotificationUri(getContext().getContentResolver(), uri);

            Log.d("Provider", "Cursor:" + cursor.toString()
                    + " - 列数:" + cursor.getColumnCount()
                    + " - 行数:" + cursor.getCount());

            返回光标;
        }

        @覆盖
        公共 int 更新(Uri uri,ContentValues 值,字符串选择,
                字符串 [] 选择参数){
            // TODO 自动生成的方法存根
            返回0;
        }

05-05 23:34:30.855: I/ActivityThread(31903): Pub com.drizzlyday.apps.episotron.providers: com.drizzlyday.apps.episotron.providers.SerieProvider
05-05 23:34:31.035: D/Serie(31903): New Serie fragment
05-05 23:34:31.040: I/LoaderManager(31903): Starting...
05-05 23:34:31.040: I/LoaderManager(31903): Creating loader...
05-05 23:34:31.055: D/AbsListView(31903): Get MotionRecognitionManager
05-05 23:34:31.060: D/AbsListView(31903): Get MotionRecognitionManager
05-05 23:34:31.080: D/dalvikvm(31903): GC_CONCURRENT freed 174K, 8% free 12340K/13383K, paused 2ms+2ms, total 16ms
05-05 23:34:31.090: D/Provider(31903): content://com.drizzlyday.apps.episotron.providers/serieprovider/episodes
05-05 23:34:31.090: D/Provider(31903): Episodes
05-05 23:34:31.100: V/SlidingMenu(31903): setting padding!
05-05 23:34:31.160: D/libEGL(31903): loaded /system/lib/egl/libEGL_mali.so
05-05 23:34:31.175: D/libEGL(31903): loaded /system/lib/egl/libGLESv1_CM_mali.so
05-05 23:34:31.175: D/libEGL(31903): loaded /system/lib/egl/libGLESv2_mali.so
05-05 23:34:31.180: D/(31903): Device driver API match
05-05 23:34:31.180: D/(31903): Device driver API version: 10
05-05 23:34:31.180: D/(31903): User space API version: 10 
05-05 23:34:31.180: D/(31903): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012 
05-05 23:34:31.210: D/OpenGLRenderer(31903): Enabling debug mode 0
05-05 23:34:31.215: V/CustomViewBehind(31903): behind INVISIBLE
05-05 23:34:31.260: D/Provider(31903): Cursor: android.database.sqlite.SQLiteCursor@41b0df88 - Columncount: 2 - Rowcount: 1
4

1 回答 1

1

我发现了问题。必须添加以下行来刷新列表的可绘制状态:

this.getListView().refreshDrawableState();
于 2013-05-06T19:52:54.840 回答