getLoaderManager().initLoader() 不接受“this"作为参数,尽管类 (ListFragment) 实现了 LoaderManager.LoaderCallbacks<Cursor> [英] getLoaderManager().initLoader() doesn't accept 'this' as argument though the class (ListFragment) implements LoaderManager.LoaderCallbacks<Cursor>
问题描述
我在遵循有关在 Android 中使用 SQLite 的指南时遇到问题.我使用的是 ListFragment
而不是 ListActivity
(如示例中所示),所以我有 ListFragment
实现 LoaderManager.LoaderCallbacks<Cursor>
代替.然后,在ListFragment
的fillData()
方法中:
I'm having trouble following a guide on using SQLite in Android. I'm using a ListFragment
instead of a ListActivity
(as in the example), so I have the ListFragment
implement LoaderManager.LoaderCallbacks<Cursor>
instead. Then, in the fillData()
method in the ListFragment
:
private void fillData() {
// Fields from the database (projection)
// Must include the _id column for the adapter to work
String[] from = new String[] { NotesSQLiteHelper.COLUMN_TITLE };
// Fields on the UI to which we map
int[] to = new int[] { R.id.label };
getLoaderManager().initLoader(0, null, this); //error
adapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.notes_row, null, from, to, 0);
setListAdapter(adapter);
}
我收到错误:
The method initLoader(int, Bundle, LoaderManager.LoaderCallbacks<D>) in the type LoaderManager is not applicable for the arguments (int, null, NotesActivity.ArrayListFragment)
在标记的行上,即使 this
实现了 LoaderManager.LoaderCallbacks
.
on the marked line even though this
implements LoaderManager.LoaderCallbacks<Cursor>
.
感谢您的任何想法.
推荐答案
您没有使用正确的 CursorLoader
和 Loader
实现.删除旧的导入并使用这些导入:
You are not using the right implementations of CursorLoader
and Loader
.
Remove your old imports and use these ones:
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;
但是我在使用 SherlockActionBar 时遇到了同样的问题:因为我必须扩展 SherlockListActivity
,所以没有方法 getSupportLoadManager()
.
But I have the same Problem using SherlockActionBar:
As I have to extend SherlockListActivity
there is NO method getSupportLoadManager()
.
对此有什么想法吗?
按照 本教程 如果您不知道如何使用片段.创建一个带有 extends SherlockFragment 的新类并将您的显示逻辑移到那里.使您的旧活动扩展 SherlockFragmentActivity 并显示新创建的 SherlockFragment.这样我就让它工作了.感谢@JakeWharton!
follow this tutorial if you do not know how to use fragments. Create a new Class with extends SherlockFragment and move your display logic there. Make your old activity extend SherlockFragmentActivity and show the newly created SherlockFragment. This way I got it working. Thanks to @JakeWharton!
这篇关于getLoaderManager().initLoader() 不接受“this"作为参数,尽管类 (ListFragment) 实现了 LoaderManager.LoaderCallbacks<Cursor>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!