我正在尝试使用数据库(Sqlite 本地数据库)实现 Cursor Loader
和 Custom Cursor Adapter
我的安卓项目。我实际上想在 Cursor Loader 和 Adapter` 的帮助下,使用本地数据库中的数据异步提供我的 ListView 。下面是我的类、子类和方法
// Home Activity class
public class HomeActivity extends FragmentActivity implements LoaderCallbacks<Cursor> {
DotCursorAdapter mAdapter;
private ListView lv;
private final int LOADER_ID = 1932;
DatabaseHandler dbHelper = new DatabaseHandler(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_activity);
lv = (ListView) findViewById(R.id.lists);
mAdapter = new DotCursorAdapter(this, null,0);
lv.setAdapter(mAdapter);
getSupportLoaderManager().initLoader(LOADER_ID, null, this);
}
@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
return new DumbLoader(this);
}
@Override
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
mAdapter.swapCursor(cursor);
}
@Override
public void onLoaderReset(Loader<Cursor> cursorLoader) {
mAdapter.swapCursor(null);
}
/**
* DumbLoader sub class
*/
public class DumbLoader extends CursorLoader {
private static final String TAG = "DumbLoader";
public DumbLoader(Context context) {
super(context);
}
@Override
public Cursor loadInBackground() {
Cursor c = dbHelper.fetchAllCountries();
return c;
}
}
/**
* DotCursor Adapter sub class
*/
public final class DotCursorAdapter extends CursorAdapter {
public DotCursorAdapter(Context context, Cursor cursor, int flags) {
super(context, cursor, 0);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// Find fields to populate in inflated template
TextView tvBody = (TextView) view.findViewById(R.id.header_text);
String body = cursor.getString(cursor.getColumnIndexOrThrow("fname"));
// Populate fields with extracted properties
tvBody.setText(body);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.list_cardview_activity, parent, false);
}
}
}
下面也是我的数据库处理程序方法,它从数据库中获取数据。
public Cursor fetchAllCountries() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor mCursor = db.query(TABLE_LOGIN, new String[] {KEY_ID,KEY_FIRSTNAME},
null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
问题
在执行上面的这段代码时,它会从日志猫中给出一条消息错误并使应用程序崩溃。
主要错误
下面也是我的日志猫详细信息
我想我的代码做错了,如果有人能帮我解决这个问题,我将不胜感激。谢谢你。
最佳答案
您有 2 种方法可以解决此问题:
1.- 创建 DumbLoader.java 文件并将其设置如下:
public class DumbLoader extends CursorLoader {
private DatabaseHandler mDBHelper;
public DumbLoader(Context context, DatabaseHandler dbHelper) {
super(context);
mDBHelper = dbHelper;
}
@Override
public Cursor loadInBackground() {
return mDBHelper.fetchAllCountries();
}
}
2.- 为您的 Activity 的 DumbLoader 嵌套类使用
static
修饰符,使其如下所示:public class HomeActivity extends FragmentActivity implements LoaderCallbacks<Cursor> {
//onCreate(), etc...
//Make DumbLoader nested class STATIC
public static class DumbLoader extends CursorLoader {
private DatabaseHandler mDBHelper;
public DumbLoader(Context context, DatabaseHandler dbHelper) {
super(context);
mDBHelper = dbHelper;
}
@Override
public Cursor loadInBackground() {
return mDBHelper.fetchAllCountries();
}
}
关于android - 从 onCreateLoader 返回的对象不能是非静态内部成员类 : DumbLoader,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32700372/