android - Why is my LoaderManager/CursorLoader combo not working correctly in displaying a database query in a ListView?
2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址:https://cloud.tencent.com/act/cps/redirect?redirect=1062
2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址:https://www.aliyun.com/minisite/goods
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.
android android-listview android-loadermanager android-cursorloader|
this question edited Jun 18 '12 at 7:02 asked Jun 18 '12 at 6:48 Andy 4,640 13 58 99
|
1 Answers
1
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.
|
this answer answered Jun 18 '12 at 7:03 Nikolay Elenkov 43.3k 6 68 73 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 7: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 7: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:
developer.android.com/guide/topics/fundamentals/loaders.html –
Nikolay Elenkov Jun 18 '12 at 7: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 1:07
|
show more comment