我有一个扩展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
我已经用Google搜索并搜索了stackoverflow,但是我没有看到任何可以提示我为何无法将其转换为String的提示。谁知道?谢谢!
我的查询的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);
}