{"id":2889,"date":"2014-03-10T02:38:48","date_gmt":"2014-03-10T02:38:48","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/10\/getting-a-castclassexception-error-in-the-onlistitemclick-method-of-a-listfragment-collection-of-common-programming-errors\/"},"modified":"2014-03-10T02:38:48","modified_gmt":"2014-03-10T02:38:48","slug":"getting-a-castclassexception-error-in-the-onlistitemclick-method-of-a-listfragment-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/10\/getting-a-castclassexception-error-in-the-onlistitemclick-method-of-a-listfragment-collection-of-common-programming-errors\/","title":{"rendered":"Getting a CastClassException error in the onListItemClick method of a ListFragment-Collection of common programming errors"},"content":{"rendered":"<p>I have a class that extends ListFragment and I&#8217;m using the Support Library. In the class&#8217;s onListItemClick method, I have this line:<\/p>\n<pre><code>@Override\npublic void onListItemClick(ListView l, View v, int position, long id) {\n    String item = (String) ((Fragment) getListAdapter().getItem(position)).getString(1);\n    DetailFrag frag = (DetailFrag) getFragmentManager().findFragmentById(R.id.frag_stitchdetail);\n    if (frag != null &amp;&amp; frag.isInLayout()) {\n        frag.setText(item);\n    }\n}\n<\/code><\/pre>\n<p>where position is a parameter that is passed to the onListItemClick method. Its value at runtime is 0 for the example app I&#8217;m building, as there&#8217;s only one item in the list. This line throws the following error:<\/p>\n<p>java.lang.CastClassException: android.content.ContentResolver$CursorWrapperInner cannot be cast to java.lang.String<\/p>\n<p>I&#8217;ve Googled this and searched on stackoverflow, but I don&#8217;t see anything that will give me a hint as to why I can&#8217;t cast this to a String. Anyone know? Thanks!<\/p>\n<p>My ContentProvider code for the query:<\/p>\n<pre><code>@Override\npublic Cursor query(Uri uri, String[] projection, String selection,\n        String[] selectionArgs, String sortOrder) {\n    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();\n    queryBuilder.setTables(STITCHTABLE_BASEPATH);\n    int uriType = sURIMatcher.match(uri);\n    switch (uriType)\n    {\n    case STITCHES_ID:\n        queryBuilder.appendWhere(SQLData.KEY_ROWID + \"=\" + uri.getLastPathSegment());\n        break;\n    case STITCHES:\n        \/\/no filter\n        break;\n    default:\n        throw new IllegalArgumentException(\"Unknown URI\");\n    }\n    Cursor cursor = queryBuilder.query(mDB.getReadableDatabase(), projection, selection, selectionArgs, null, null, sortOrder);\n    cursor.setNotificationUri(getContext().getContentResolver(), uri);\n    return cursor;\n}\n<\/code><\/pre>\n<p>The code in my ListFragment for the CursorLoader:<\/p>\n<pre><code>@Override\npublic void onActivityCreated(Bundle savedInstanceState) {\n    super.onActivityCreated(savedInstanceState);\n\n    Intent myData = getActivity().getIntent();\n    Bundle info = myData.getExtras();\n\n    String[] dataColumns = { \"stitchname\" };\n    int[] viewIDs = { R.id.stitchlist1 };\n    mAdapter = new SimpleCursorAdapter(getActivity(), R.layout.stitchlist, null, dataColumns, viewIDs, 0);\n    setListAdapter(mAdapter);\n    getLoaderManager().initLoader(0, info, (LoaderCallbacks) this); \n\n}\n@Override\npublic Loader onCreateLoader(int id, Bundle args) {\n    String selection = \"stitchlevel=?\";\n    String[] selectionArgs = new String[] {args.getString(\"Level\")};\n    return (Loader) new CursorLoader(getActivity(), STITCHES_URI,\n            PROJECTION, selection, selectionArgs, null);    \n}\n@Override\npublic void onLoadFinished(Loader loader, Cursor cursor) {\n        mAdapter.swapCursor((android.database.Cursor) cursor);      \n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have a class that extends ListFragment and I&#8217;m using the Support Library. In the class&#8217;s onListItemClick method, I have this line: @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 &amp;&amp; frag.isInLayout()) { frag.setText(item); } } [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2889","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2889","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2889"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2889\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2889"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2889"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2889"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}