{"id":5731,"date":"2014-04-06T10:30:17","date_gmt":"2014-04-06T10:30:17","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/06\/how-to-properly-implement-a-onclickitem-from-a-search-activitys-listview-collection-of-common-programming-errors\/"},"modified":"2014-04-06T10:30:17","modified_gmt":"2014-04-06T10:30:17","slug":"how-to-properly-implement-a-onclickitem-from-a-search-activitys-listview-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/06\/how-to-properly-implement-a-onclickitem-from-a-search-activitys-listview-collection-of-common-programming-errors\/","title":{"rendered":"How to properly implement a onClickItem from a search activity&#39;s listview-Collection of common programming errors"},"content":{"rendered":"<p>I have a search activity (ActivitySearch.java) the correctly returns results when I use the soft keyboards &#8216;Go&#8217; button, it returns all result from the search suggestions. I using a Content Provider and Cursor LoaderCallbacks. This is my first time trying to LoaderCallbacks and doing a search activity.<\/p>\n<p>Now I want to be able to click on one of the suggested results via the onClickItem\/onClickItemListenter, and have it return to the search listview for final choice by the user, but I know my code isn&#8217;t right, obviously. FYI, I&#8217;ve enabled all my activities in the project as searchable. I&#8217;ve looked at a bunch of samples but I haven&#8217;t been able to glean the correct method.<\/p>\n<p>I&#8217;m using a simple activity (ActivityFloor.java) that I&#8217;m hitting the hardware search key on, just a few buttons, that kick off intents.<\/p>\n<p>My search activity uses the default android view for the listview results. It also inherits it&#8217;s activity from MyListActivity, but this is for supporting a common menu.<\/p>\n<p>Here&#8217;s my manifest:<\/p>\n<pre><code>\n\n\n\n\n\n\n\n\n\n\n    \n\n    \n    \n    \n    \n    \n    \n    \n\n    \n\n    \n        \n            \n\n            \n        \n    \n\n    \n    \n\n\n    \n\n    \n        \n            \n\n            \n        \n\n        \n    \n\n\n\n<\/code><\/pre>\n<p>Here&#8217;s my searchable.xml in res\/xml<\/p>\n<pre><code>\n\n\n<\/code><\/pre>\n<p>Here&#8217;s my search activity (ActivitySearch.java):<\/p>\n<pre><code>package com.birdsall.tda;\n\nimport android.app.Activity;\nimport android.app.LoaderManager;\nimport android.app.SearchManager;\nimport android.content.ContentUris;\nimport android.content.CursorLoader;\nimport android.content.Intent;\nimport android.content.Loader;\nimport android.database.Cursor;\nimport android.net.Uri;\nimport android.os.Bundle;\nimport android.util.Log;\nimport android.view.View;\nimport android.view.View.OnClickListener;\nimport android.widget.AdapterView;\nimport android.widget.ListView;\nimport android.widget.SimpleCursorAdapter;\nimport android.widget.Toast;\nimport android.widget.AdapterView.OnItemClickListener;\n\npublic class ActivitySearch extends MyListActivity implements \n    LoaderManager.LoaderCallbacks {\n\n\nprivate static String QUERY_EXTRA_KEY = \"QUERY_EXTRA_KEY\";\n\nprivate SimpleCursorAdapter adapter;\nprivate final String TAG = \"ActivitySearch\";\n\n@Override\npublic void onCreate(Bundle savedInstanceState) {\n    super.onCreate(savedInstanceState);\n    Log.i(TAG, \"onCreate\");\n\n    View mListView  = getListView();\n\n    Log.i(TAG, \"onCreate ... after setOnItemClickListener\");\n\n    Toast.makeText(getApplicationContext(), \"onCreate ... after setOnItemClickListener\", Toast.LENGTH_LONG).show();\n\n    \/\/ Create a new adapter and bind it to the List View\n    adapter = new SimpleCursorAdapter(this,\n            android.R.layout.simple_list_item_1, null,\n            new String[] { TDAdb.COL_RULETITLE },\n            new int[] { android.R.id.text1 }, 0);\n    setListAdapter(adapter);\n\n    \/\/ Initiate the Cursor Loader\n    getLoaderManager().initLoader(0, null, this);\n\n    \/\/ Get the launch Intent\n    parseIntent(getIntent());\n}\n\n@Override\nprotected void onNewIntent(Intent intent) {\n    super.onNewIntent(intent);\n    Log.i(TAG, \"onNewIntent\");\n    Toast.makeText(getApplicationContext(), \"onNewIntent\", Toast.LENGTH_LONG).show();\n    parseIntent(getIntent());\n\n}\n\n\n\nprivate void parseIntent(Intent intent) {\n    \/\/ If the Activity was started to service a Search request,\n    \/\/ extract the search query.\n    Log.i(TAG, \"parseIntent\");\n    Toast.makeText(getApplicationContext(), \"parseIntent\", Toast.LENGTH_LONG).show();\n    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {\n        String searchQuery = intent.getStringExtra(SearchManager.QUERY);\n\n        \/\/ Perform the search, passing in the search query as an argument\n        \/\/ to the Cursor Loader\n        Bundle args = new Bundle();\n        args.putString(QUERY_EXTRA_KEY, searchQuery);\n\n        \/\/ Restart the Cursor Loader to execute the new query.\n        getLoaderManager().restartLoader(0, args, this);\n    }\n}\n\npublic Loader onCreateLoader(int id, Bundle args) {\n    Log.i(TAG, \"onCreateLoader\");\n    Toast.makeText(getApplicationContext(), \"onCreateLoader\", Toast.LENGTH_LONG).show();\n    String query = \"0\";\n\n    if (args != null) {\n        \/\/ Extract the search query from the arguments.\n        query = args.getString(QUERY_EXTRA_KEY);\n    }\n\n    \/\/ Construct the new query in the form of a Cursor Loader.\n    String[] projection = { TDAdb.KEY_ROWID, TDAdb.COL_RULETITLE };\n    String where = TDAdb.COL_RULETITLE + \" LIKE \\\"%\" + query + \"%\\\"\";\n    String[] whereArgs = null;\n    String sortOrder = TDAdb.COL_RULETITLE;\n\n    \/\/ Create the new Cursor loader.\n    return new CursorLoader(this, TDAProvider.CONTENT_URI_RULES,\n            projection, where, whereArgs, sortOrder);\n}\n\npublic void onLoadFinished(Loader loader, Cursor cursor) {\n    \/\/ Replace the result Cursor displayed by the Cursor Adapter with\n    \/\/ the new result set.\n    Log.i(TAG, \"onLoadFinished\");\n    Toast.makeText(getApplicationContext(), \"onLoadFinished\", Toast.LENGTH_LONG).show();\n\n    if (adapter == null) {\n        Log.i(TAG, \"onLoadFinished ... adapter is NULL\");\n        Toast.makeText(getApplicationContext(), \"onLoadFinished ... adapter is NULL\", Toast.LENGTH_LONG).show();\n        this.finish();\n    } \n    Log.i(TAG, \"onLoadFinished ... adapter is valued\");\n    Toast.makeText(getApplicationContext(), \"onLoadFinished ... adapter is valued\", Toast.LENGTH_LONG).show();\n\n    adapter.swapCursor(cursor);     \n\n}\n\npublic void onLoaderReset(Loader loader) {\n    \/\/ Remove the existing result Cursor from the List Adapter.\n    Log.i(TAG, \"onLoaderReset\");\n    Toast.makeText(getApplicationContext(), \"onLoaderReset\", Toast.LENGTH_LONG).show();\n    adapter.swapCursor(null);\n}\n\nprivate void handleIntent(Intent intent) {\n    Log.i(TAG, \"handleIntent\");\n    Toast.makeText(getApplicationContext(), \"handleIntent\", Toast.LENGTH_LONG).show();\n    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {\n        \/\/ Gets the search query from the voice recognizer intent\n        String query = intent.getStringExtra(SearchManager.QUERY);\n\n        \/\/ Set the search box text to the received query and submit the\n        \/\/ search\n        \/\/ mSearchView.setQuery(query, true);\n    }\n}\n\n  @Override\n  protected void onListItemClick(ListView listView, View view, int position, long id) {\n    super.onListItemClick(listView, view, position, id);\n    Toast.makeText(getApplicationContext(), \n    \" search listview position:\" + position,\n    Toast.LENGTH_LONG).show();\n    \/\/ Create a URI to the selected item.\n    Uri selectedUri = \n      ContentUris.withAppendedId(TDAProvider.CONTENT_URI, id);\n\n    \/\/ Create an Intent to view the selected item.\n    Intent intent = new Intent(Intent.ACTION_VIEW);\n    intent.setData(selectedUri);\n\n    \/\/ Start an Activity to view the selected item.\n    startActivity(intent);\n  }\n\n}\n<\/code><\/pre>\n<p>I NO LONGER GET ERRORS, THANKS TO Micheal (below), So the LOGCAT errors can be ignored.<\/p>\n<p>I get errors, but only after trying to implement the onClickItem coding from my logcat which I&#8217;ve included for completeness.<\/p>\n<pre><code>06-2923:56:57.416: I\/TDAProvider(13786): query \n06-2923:56:57.439: W\/SuggestionsAdapter(13786): Search suggestions query threw an exception.\n06-2923:56:57.439: W\/SuggestionsAdapter(13786): java.lang.IndexOutOfBoundsException\n06-2923:56:57.439: W\/SuggestionsAdapter(13786):     at android.net.Uri$PathSegments.get(Uri.java:978)\n06-2923:56:57.439: W\/SuggestionsAdapter(13786):     at android.net.Uri$PathSegments.get(Uri.java:963)\n06-2923:56:57.439: W\/SuggestionsAdapter(13786):     at com.birdsall.tda.TDAProvider.query(TDAProvider.java:180)\n06-2923:56:57.439: W\/SuggestionsAdapter(13786):     at android.content.ContentProvider.query(ContentProvider.java:652)\n06-2923:56:57.439: W\/SuggestionsAdapter(13786):     at android.content.ContentProvider$Transport.query(ContentProvider.java:189)\n06-2923:56:57.439: W\/SuggestionsAdapter(13786):     at android.content.ContentResolver.query(ContentResolver.java:370)\n06-2923:56:57.439: W\/SuggestionsAdapter(13786):     at android.content.ContentResolver.query(ContentResolver.java:313)\n06-2923:56:57.439: W\/SuggestionsAdapter(13786):     at android.app.SearchManager.getSuggestions(SearchManager.java:823)\n06-2923:56:57.439: W\/SuggestionsAdapter(13786):     at android.widget.SuggestionsAdapter.runQueryOnBackgroundThread(SuggestionsAdapter.java:190)\n06-2923:56:57.439: W\/SuggestionsAdapter(13786):     at android.widget.CursorFilter.performFiltering(CursorFilter.java:49)\n06-2923:56:57.439: W\/SuggestionsAdapter(13786):     at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)\n06-2923:56:57.439: W\/SuggestionsAdapter(13786):     at android.os.Handler.dispatchMessage(Handler.java:99)\n06-2923:56:57.439: W\/SuggestionsAdapter(13786):     at android.os.Looper.loop(Looper.java:137)\n06-2923:56:57.439: W\/SuggestionsAdapter(13786):     at android.os.HandlerThread.run(HandlerThread.java:60)\n06-2923:57:00.002: I\/TDAProvider(13786): query \n06-2923:57:00.049: I\/TDAProvider(13786): query return cursor \n06-2923:57:00.635: I\/TDAProvider(13786): query \n06-2923:57:00.635: I\/TDAProvider(13786): query return cursor \n06-2923:57:01.275: I\/TDAProvider(13786): query \n06-2923:57:01.275: I\/TDAProvider(13786): query return cursor \n06-2923:57:02.650: W\/InputEventReceiver(13786): Attempted to finish an input event but the input event receiver has already been disposed.\n06-2923:57:02.689: I\/ActivitySearch(13786): onCreate\n06-2923:57:02.728: D\/AndroidRuntime(13786): Shutting down VM\n06-2923:57:02.728: W\/dalvikvm(13786): threadid=1: thread exiting with uncaught exception (group=0x40e1b2a0)\n06-2923:57:02.728: E\/AndroidRuntime(13786): FATAL EXCEPTION: main\n06-2923:57:02.728: E\/AndroidRuntime(13786): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.birdsall.tda\/com.birdsall.tda.ActivitySearch}: java.lang.ClassCastException: com.birdsall.tda.ActivitySearch cannot be cast to android.view.View$OnClickListener\n06-2923:57:02.728: E\/AndroidRuntime(13786):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2136)\n06-2923:57:02.728: E\/AndroidRuntime(13786):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2174)\n06-2923:57:02.728: E\/AndroidRuntime(13786):     at android.app.ActivityThread.access$700(ActivityThread.java:141)\n06-2923:57:02.728: E\/AndroidRuntime(13786):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1267)\n06-2923:57:02.728: E\/AndroidRuntime(13786):     at android.os.Handler.dispatchMessage(Handler.java:99)\n06-2923:57:02.728: E\/AndroidRuntime(13786):     at android.os.Looper.loop(Looper.java:137)\n06-2923:57:02.728: E\/AndroidRuntime(13786):     at android.app.ActivityThread.main(ActivityThread.java:5059)\n06-2923:57:02.728: E\/AndroidRuntime(13786):     at java.lang.reflect.Method.invokeNative(Native Method)\n06-2923:57:02.728: E\/AndroidRuntime(13786):     at java.lang.reflect.Method.invoke(Method.java:511)\n06-2923:57:02.728: E\/AndroidRuntime(13786):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)\n06-2923:57:02.728: E\/AndroidRuntime(13786):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)\n06-2923:57:02.728: E\/AndroidRuntime(13786):     at dalvik.system.NativeStart.main(Native Method)\n06-2923:57:02.728: E\/AndroidRuntime(13786): Caused by: java.lang.ClassCastException: com.birdsall.tda.ActivitySearch cannot be cast to android.view.View$OnClickListener\n06-2923:57:02.728: E\/AndroidRuntime(13786):     at com.birdsall.tda.ActivitySearch.onCreate(ActivitySearch.java:39)\n06-2923:57:02.728: E\/AndroidRuntime(13786):     at android.app.Activity.performCreate(Activity.java:5058)\n06-2923:57:02.728: E\/AndroidRuntime(13786):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)\n06-2923:57:02.728: E\/AndroidRuntime(13786):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)\n06-2923:57:02.728: E\/AndroidRuntime(13786):     ... 11 more\n06-2923:57:04.392: I\/Process(13786): Sending signal. PID: 13786 SIG: 9\n<\/code><\/pre>\n<p>Thanks for the time and help, in advance.<\/p>\n<p>Here&#8217;s the gist of MyActivity, just a common menu for my Activities.<\/p>\n<pre><code>package com.birdsall.tda;\n\nimport android.app.ListActivity;\nimport android.content.Intent;\nimport android.view.Menu;\nimport android.view.MenuItem;\n\npublic class MyListActivity extends ListActivity {\n\nString selectParam = \"\";\n\n@Override\npublic boolean onCreateOptionsMenu(Menu menu) {\n    getMenuInflater().inflate(R.menu.main, menu);\n    return true;\n}\n\n@Override\npublic boolean onOptionsItemSelected(MenuItem item) {\n    switch(item.getItemId()) {\n    case android.R.id.home:\n          Intent intent = new Intent(this, ActivityMain.class);\n          intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);\n          startActivity(intent);\n          return true;\n\n    case R.id.am_index:\n        Intent i1 = new Intent(this, ActivityIndex.class);\n        startActivity(i1);\n        return true;    \n\n        \/*  ...    More menu items *\/\n\n    default:\n        return super.onOptionsItemSelected(item);\n    }\n\n}\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have a search activity (ActivitySearch.java) the correctly returns results when I use the soft keyboards &#8216;Go&#8217; button, it returns all result from the search suggestions. I using a Content Provider and Cursor LoaderCallbacks. This is my first time trying to LoaderCallbacks and doing a search activity. Now I want to be able to click [&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-5731","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5731","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=5731"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5731\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}