我正在尝试使用LoaderCallBacks从数据库加载数据。但是不会调用loadercallbacks中的onLoadFinished函数。因此,您可以在日志中看到返回的行数为1但列表中没有显示任何条目,也未调用onLoadFinished,如您所见。
SerieFragment
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); }
的ContentProvider
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; }
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
答案 0 :(得分:1)
我发现了问题。必须添加以下行来刷新列表的可绘制状态:
this.getListView().refreshDrawableState();