我正在尝试使用CursorLoader获取设备中包含电话号码的联系人列表,如下所示:
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CursorLoader(getActivity(), ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[] {
Contacts._ID, Contacts.DISPLAY_NAME }, null, null, Contacts.DISPLAY_NAME);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
mAdapter.swapCursor(cursor);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
这完全有时,有时不工作。它从一个应用程序启动变为另一个。我得到的异常的堆栈跟踪发布在下面。我将光标传递给IndexedNameAdapter
这是SimpleCursorAdapter
的扩展名,其中包含AlphabetIndexer
以便快速滚动:
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.SimpleCursorAdapter;
import android.widget.AlphabetIndexer;
import android.widget.SectionIndexer;
public class IndexedNameAdapter extends SimpleCursorAdapter implements SectionIndexer {
private AlphabetIndexer mAlphabetIndexer;
private String[] from;
public IndexedNameAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
this(context, layout, c, from, to, 0);
}
public IndexedNameAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
this.from = from;
setAlphabetIndexerCursor(c, from);
}
@Override
public Cursor swapCursor(Cursor c) {
setAlphabetIndexerCursor(c, from);
// error occurs here:
return super.swapCursor(c);
}
private void setAlphabetIndexerCursor(Cursor c, String[] columns) {
if (c != null) {
mAlphabetIndexer = new AlphabetIndexer(c, c.getColumnIndex(columns[0]), "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
mAlphabetIndexer.setCursor(c);
}
}
@Override
public int getPositionForSection(int section) {
return mAlphabetIndexer.getPositionForSection(section);
}
@Override
public int getSectionForPosition(int position) {
return mAlphabetIndexer.getSectionForPosition(position);
}
@Override
public Object[] getSections() {
return mAlphabetIndexer.getSections();
}
}
以下是我看到的错误:
java.lang.IllegalArgumentException: column 'display_name' does not exist
at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
at android.database.CursorWrapper.getColumnIndexOrThrow(CursorWrapper.java:78)
at android.support.v4.widget.SimpleCursorAdapter.findColumns(SimpleCursorAdapter.java:317)
at android.support.v4.widget.SimpleCursorAdapter.swapCursor(SimpleCursorAdapter.java:328)
at com.mypackage.myprofile.IndexedNameAdapter.swapCursor(IndexedNameAdapter.java:29)
at com.mypackage.myprofile.ProfileUserFragment.onLoadFinished(ProfileUserFragment.java:52)
at com.mypackage.myprofile.ProfileUserFragment.onLoadFinished(ProfileUserFragment.java:1)
at android.support.v4.app.LoaderManagerImpl$LoaderInfo.callOnLoadFinished(LoaderManager.java:427)
at android.support.v4.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManager.java:395)
at android.support.v4.content.Loader.deliverResult(Loader.java:103)
at android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:81)
at android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:35)
at android.support.v4.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:221)
at android.support.v4.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:61)
at android.support.v4.content.ModernAsyncTask.finish(ModernAsyncTask.java:461)
at android.support.v4.content.ModernAsyncTask.access$500(ModernAsyncTask.java:47)
at android.support.v4.content.ModernAsyncTask$InternalHandler.handleMessage(ModernAsyncTask.java:474)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5039)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
我通常不喜欢回答我自己的问题,但我最终找到了解决问题的方法。
我在同一个活动中有另一个片段也实现了LoaderCallbacks<Cursor>
,并且我没有两个游标的唯一ID。我正在为这个片段中的错误加载器获取一个游标。
我认为,因为它们是两个不同的片段,在同一时间永远不可见,我不需要一个唯一的ID,但这是不正确的。