0

I have been working all day on this, and have been trying to understand how it all fits together and reworking my code to fit together well.
So I will show you what I have. There is no error, just blank.

MyAgendaLoaderManager.java:

public class MyAgendaLoaderManager implements LoaderCallbacks<Cursor>{

MyAgendaAdapter agendaAdapter;
Context mContext;
String date;

public MyAgendaLoaderManager(Context context, MyAgendaAdapter adapter, String date) {
    agendaAdapter = adapter;
    mContext = context;
    this.date = date;
}

@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
    Uri baseUri = SmartCalProvider.CONTENT_URI;
    return new CursorLoader(mContext, baseUri, 
            new String[] {"_id, event_name, start_date, start_time, end_date, end_time, location"}, 
            "WHERE date(?) >= start_date and date(?) <= end_date", new String[]{date, date}, null);
}

@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
    // TODO Auto-generated method stub
    agendaAdapter.swapCursor(arg1);
}

@Override
public void onLoaderReset(Loader<Cursor> arg0) {
    // TODO Auto-generated method stub
    agendaAdapter.swapCursor(null);
}

}

CalProvider.java:

public class SmartCalProvider extends ContentProvider {
public static final String AUTHORITY = "content://com.smartcal.eventprovider";
public static final Uri CONTENT_URI = Uri.parse(AUTHORITY);

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "cal.db";

private SmartCalOpenHelper openHelper;
private SQLiteDatabase database;

@Override
public boolean onCreate() {
    openHelper = new SmartCalOpenHelper(getContext());
    return true;
}


@Override
public String getType(Uri uri) {
    return null;
}

@Override
public Cursor query(Uri uri, String[] projection, String selection,
        String[] selectionArgs, String sortOrder) {
    database = openHelper.getWritableDatabase();
    return database.query("events_info", projection, selection, selectionArgs, null, null, null);
}

@Override
public Uri insert(Uri uri, ContentValues values) {
    return null;
}

@Override
public int delete(Uri arg0, String arg1, String[] arg2) {
    return 0;
}


@Override
public int update(Uri uri, ContentValues values, String selection,
        String[] selectionArgs) {
    return 0;
}
}

And if it matters, heres the code that runs it in the main activity:

agendaAdapter = new MyAgendaAdapter(this, null);
MyAgendaLoaderManager loader = new MyAgendaLoaderManager(this, agendaAdapter, getChosenDate());

I just don't see how it blanks. Please keep in mind I intentionally left some stuff blank in the CursorLoader, and LoaderManager because I did not want to fill it all in to find out there was an error, so I was just testing to see if the initial list was display, and it was not. Any help figuring out what I did wrong would be much appreciated.

EDIT: Actually, now that I think about it, there is nothing that actually ties what I am doing to a specific list besides when my adapter makes the view that holds it. But that view isn't part of the regular layout. So maybe thats an error I have, unfortunately I have no idea how to do so.

Andy
  • 10,553
  • 21
  • 75
  • 125

1 Answers1

2

You shouldn't instantiate a loader directly. You need to go through the activities getLoaderManager() method for it to be properly initialized and started. So from your activity call getLoaderManager().initLoader()/restartLoader() as needed.

Nikolay Elenkov
  • 52,576
  • 10
  • 84
  • 84
  • In my case, because I have 3 parameters I pass it, how can I pass them with what you are saying? But it rings a bell while going through research on how to get this done – Andy Jun 18 '12 at 07:04
  • Actually, the 3rd parameter in `initLoader()`, how would one look like. I have no idea exactly how to do this. None of the examples show that being done. Just what I've done currently. – Andy Jun 18 '12 at 07:14
  • Have you read the official reference? The third parameter is a `LoaderManager.LoaderCallbacks`, typically you implement it in your activity or fragment, so you can just pass `this`. Check out the official docs: http://developer.android.com/guide/topics/fundamentals/loaders.html – Nikolay Elenkov Jun 18 '12 at 07:31
  • Ok, so I have been looking at it. If I use `initLoader()`, how can I pass in the arguments I need for my `MyAgendaLoaderManager` class? And how can I ensure this is posted in my `ListView`. I have a `GridView` in my main activity as well, and I don't really see this being connected anywhere. Should I do `list.setAdapter(agendaAdapter)` in my main activity? – Andy Jun 18 '12 at 20:18
  • You can use the `Bundle args` parameter to pass arguments. Loader only return data, how you use it is up to you. If you are getting back a cursor, use a `CursorAdapter` and set it to your view as necessary. – Nikolay Elenkov Jun 19 '12 at 01:07
  • @Andy You wouldn't pass arguments TO your MyAgendaLoaderManager class FROM the initLoader() method. It is the OTHER WAY AROUND - you instantiate MyAgendaLoaderManager with arguments in the constructor, and then pass these arguments into initLoader(int id, Bundle args, LoaderManager.LoaderCallbacks callback). – IgorGanapolsky Jan 23 '14 at 19:46