Search code examples
androiddatabaseandroid-contentproviderandroid-cursorloader

CursorLoader not showing DB entires


I've got a CursorLoader in a ListFragment (that is inside a ViewPager) that queries a database. I have a content provider for that database, which I've verified works.

The issue is this: when the app runs for the very first time a separate service calls a bulk insert in a ContentProvider:

public int bulkInsert(Uri uri, ContentValues[] values) {
    if(LOGV) Log.v(TAG, "insert(uri=" + uri + ", values" + values.toString() + ")");

    final SQLiteDatabase db = openHelper.getWritableDatabase();
    final int match = uriMatcher.match(uri);

    switch(match) {
        case SNAP: {
            db.beginTransaction();
            for(ContentValues cv : values) {
                db.insertOrThrow(Tables.SNAP, null, cv);                        
            }
            db.setTransactionSuccessful();
            db.endTransaction();

            getContext().getApplicationContext().getContentResolver().notifyChange(uri, null);
            return values.length;
        }

The CursorLoader in the list fragment returns 0 rows though on the very first run (when the database gets created). If I close and restart the app then the CursorLoader works great and returns exactly what I need. I've tried to implement waiting via a handler, but it doesn't seem to help. Here is the ListFragment that utilizes the CursorLoader:

public class DataBySnapFragment extends ListFragment implements LoaderCallbacks<Cursor> {   
    public static final String TAG = "DataBySnapFragment";

    protected Cursor cursor = null;
    private DataBySnapAdapter adapter;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Log.d(TAG, "RELOADING!!!!!");
            onLoadDelay();
        }
    };  

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.d(TAG, "onActivityCreated");

        adapter = new DataBySnapAdapter(getActivity(), 
                                        R.layout.list_item_databysnap, 
                                        null, 
                                        new String[]{}, 
                                        new int[]{}, 
                                        0);
        setListAdapter(adapter);        
        getLoaderManager().initLoader(0, null, this);       
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_databysnap, null);
        return view;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("error_workaround_1",
                           "workaroundforerror:Issue 19917,
                           http://code.google.com/p/android/issues/detail?id=19917");
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        CursorLoader cursorLoader = new CursorLoader(getActivity(),
                                                     Snap.CONTENT_URI,
                                                     null,
                                                     null,
                                                     null,
                                                     Snap.DATA_MONTH_TO_DATE + " DESC LIMIT 6");

        return cursorLoader;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        Log.d(TAG, "data rows: " + data.getCount());
        if(data.getCount() <= 0) {
            delayThread();
        } else {
            adapter.swapCursor(data);           
        }
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        adapter.swapCursor(null);
    }

    private void onLoadDelay() {
        getLoaderManager().initLoader(0, null, this);       
    }

    private void delayThread() {
        new Thread() {
            public void run() {
                longTimeMethod();
                handler.sendEmptyMessage(0);
            }
        }.start();
    }

    private void longTimeMethod() {
        try {
            Thread.sleep(12000);
        } catch (InterruptedException e) {
            Log.e("tag", e.getMessage());
        }
    }   
}

Can anyone let me know why this might be happening, or at least steer me in the right direction? Thanks!


Solution

  • Unless you were stalling the main thread, making a new thread and telling it to sleep wouldn't really solve anything.

    I can't really say what exactly might be wrong, but it sounds like you might need to refresh your data. To test you could try to make a button with an OnClickListener which refreshes the data content, and re-pull it from the database.