0

我有一个扩展 ListFragment 的类,我正在使用支持库。在类的 onListItemClick 方法中,我有这一行:

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    String item = (String) ((Fragment) getListAdapter().getItem(position)).getString(1);
    DetailFrag frag = (DetailFrag) getFragmentManager().findFragmentById(R.id.frag_stitchdetail);
    if (frag != null && frag.isInLayout()) {
        frag.setText(item);
    }
}

其中 position 是传递给 onListItemClick 方法的参数。对于我正在构建的示例应用程序,它在运行时的值为 0,因为列表中只有一项。此行引发以下错误:

java.lang.CastClassException: android.content.ContentResolver$CursorWrapperInner 不能转换为 java.lang.String

我已经用谷歌搜索了这个并在stackoverflow上进行了搜索,但是我没有看到任何可以提示我为什么不能将它转换为字符串的东西。有人知道吗?谢谢!

我的 ContentProvider 查询代码:

@Override
public Cursor query(Uri uri, String[] projection, String selection,
        String[] selectionArgs, String sortOrder) {
    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
    queryBuilder.setTables(STITCHTABLE_BASEPATH);
    int uriType = sURIMatcher.match(uri);
    switch (uriType)
    {
    case STITCHES_ID:
        queryBuilder.appendWhere(SQLData.KEY_ROWID + "=" + uri.getLastPathSegment());
        break;
    case STITCHES:
        //no filter
        break;
    default:
        throw new IllegalArgumentException("Unknown URI");
    }
    Cursor cursor = queryBuilder.query(mDB.getReadableDatabase(), projection, selection, selectionArgs, null, null, sortOrder);
    cursor.setNotificationUri(getContext().getContentResolver(), uri);
    return cursor;
}

我的 ListFragment 中 CursorLoader 的代码:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    Intent myData = getActivity().getIntent();
    Bundle info = myData.getExtras();

    String[] dataColumns = { "stitchname" };
    int[] viewIDs = { R.id.stitchlist1 };
    mAdapter = new SimpleCursorAdapter(getActivity(), R.layout.stitchlist, null, dataColumns, viewIDs, 0);
    setListAdapter(mAdapter);
    getLoaderManager().initLoader(0, info, (LoaderCallbacks<Cursor>) this); 

}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    String selection = "stitchlevel=?";
    String[] selectionArgs = new String[] {args.getString("Level")};
    return (Loader<Cursor>) new CursorLoader(getActivity(), STITCHES_URI,
            PROJECTION, selection, selectionArgs, null);    
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        mAdapter.swapCursor((android.database.Cursor) cursor);      
}
4

0 回答 0