关于android:Loadermanager onLoadFinished没有调用

Loadmanager onLoadFinished not called

我正试图用loaderCallbacks从数据库加载数据。但不会调用LoaderCallbacks中的OnLoadFinished函数。因此,正如您在日志中看到的那样,返回的行数为1,但列表中没有显示任何条目,而且也没有如您所看到的那样调用OnLoadFinished。

血清碎片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
    public class SerieFragment extends SherlockListFragment implements
            LoaderCallbacks {

        private SimpleCursorAdapter adapter;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

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

            Log.i("LoaderManager","Starting...");
            getLoaderManager().initLoader(0, null, this);

            adapter = new SimpleCursorAdapter(getActivity(),
                    R.layout.serie_list_item, null, from, to, 0);

            this.setListAdapter(adapter);
        }

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

        }

        @Override
        public Loader onCreateLoader(int id, Bundle args) {
            Log.i("LoaderManager","Creating loader...");

            String[] projection = { DBContract.Episodes._ID,
                    DBContract.Episodes.NAME };
            CursorLoader cursorLoader = new CursorLoader(this.getActivity(),
                    Uri.parse(SerieProvider.CONTENT_URI +"/episodes"), projection,
                    null, null, null);
            return cursorLoader;
        }

        @Override
        public void onLoadFinished(Loader loader, Cursor cursor) {
            Log.i("LoaderManager",
                   "Finished load entry... - Cursor:" + cursor.getCount());
            adapter.swapCursor(cursor);
        }

        @Override
        public void onLoaderReset(Loader loader) {
            Log.i("LoaderManager","Resetting loader...");
            adapter.swapCursor(null);
        }

内容提供者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
    public class SerieProvider extends ContentProvider {

        private static DatabaseHelper database;
        private static final String PROVIDER ="com.drizzlyday.apps.episotron.providers";
        private static final String BASE_PATH ="serieprovider";
        public static final Uri CONTENT_URI = Uri.parse("content://" + PROVIDER
                +"/" + BASE_PATH);

        public static final int SERIES = 1;
        public static final int EPISODES = 2;

        private static final UriMatcher uriMatcher;
        static {
            uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
            uriMatcher.addURI(PROVIDER, BASE_PATH, SERIES);
            uriMatcher.addURI(PROVIDER, BASE_PATH +"/episodes", EPISODES);
        }

        @Override
        public boolean onCreate() {
            database = new DatabaseHelper(getContext());
            return false;
        }

        @Override
        public int delete(Uri uri, String selection, String[] selectionArgs) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public String getType(Uri uri) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public Uri insert(Uri uri, ContentValues values) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public Cursor query(Uri uri, String[] projection, String selection,
                String[] selectionArgs, String sortOrder) {

            Log.d("Provider", uri.toString());
            SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();

            int uriType = uriMatcher.match(uri);
            switch (uriType) {
            case SERIES:
                Log.d("Provider","Series");
                queryBuilder.setTables(DBContract.Series.TABLE_NAME);
                break;
            case EPISODES:
                Log.d("Provider","Episodes");
                queryBuilder.setTables(DBContract.Episodes.TABLE_NAME);
                break;
            default:
                throw new IllegalArgumentException("Unknown URI:" + uri);
            }

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

            Log.d("Provider","Cursor:" + cursor.toString()
                    +" - Columncount:" + cursor.getColumnCount()
                    +" - Rowcount:" + cursor.getCount());

            return cursor;
        }

        @Override
        public int update(Uri uri, ContentValues values, String selection,
                String[] selectionArgs) {
            // TODO Auto-generated method stub
            return 0;
        }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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

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

1
this.getListView().refreshDrawableState();