0

我正在尝试使用 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)
4

1 回答 1

2

我通常不喜欢回答我自己的问题,但我最终确实找到了解决问题的方法。

我在同一个活动中有另一个片段也实现了LoaderCallbacks<Cursor>,我没有两个游标的唯一 ID。我在这个片段中得到了错误加载器的光标。

我认为因为它们位于两个永远不会同时可见的不同片段中,所以我不需要唯一 ID,但这是不正确的。

于 2013-02-01T21:32:11.923 回答