Reputation: 23
I am using an SQLite database and wish to load it without using a ContentProvider
.
I am having trouble getting my subclassed SimpleCursorLoader (taken from CursorLoader usage without ContentProvider) to work with the LoaderManager
.
In the overwritten method
@Override
public Loader<Cursor> onCreateLoader(int ID, Bundle args) {
return new ListCursorLoader(this, dBHelper);
}
I get a type mismatch saying that it cannot convert from ListCursorLoader
to Loader<Cursor>
. I have tried creating the ListCursorLoader
on the fly (that is, in the method), but this does not work either.
Here is the code for my ListCursorLoader
:
package utilities;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
public class ListCursorLoader extends SimpleCursorLoader {
private DBAdapter dBAdapter;
public ListCursorLoader(Context context, DBAdapter adapter) {
super(context);
dBAdapter = adapter;
}
@Override
public Cursor loadInBackground() {
Cursor cursor = null;
dBAdapter.open();
try {
cursor = dBAdapter.getAllQueries();
} catch (SQLException e) {
e.printStackTrace();
}
if (cursor != null) {
cursor.getCount();
}
return cursor;
}
}
As you can see I have only overwritten the loadInBackground()
method, and I simply cannot see what I am doing wrong.
Hope you guys can help!
Upvotes: 1
Views: 3814
Reputation: 1106
I just tried your code and it worked without an issue.
You should double-check your imports. The SimpleCursorLoader you linked to is using the support library. You didn't provide the code, but I think you may be using the default LoaderManager, not the one provided by the support library.
So for you to be able to use this class you need to reference android.support.v4.content.Loader
and load it using the SupportLoaderManager
in your Fragment.
Here is the code from my Fragment that worked: (Note the use of the support library.)
import android.database.Cursor;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
...
public class MainActivity extends FragmentActivity implements LoaderCallbacks<Cursor>{
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.getSupportLoaderManager().initLoader(0, null, this);
}
...
@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
return new ListCursorLoader(this);
}
...
}
Upvotes: 7