title: What am I doing wrong in the implementation of the Android search?
body: <p>Please tell what is wrong with the code?. The task is to implement the search in the directory. I mentioned the SEARCH in the manifest file for current MainActivity.java (it is a searchable activity).
  Is it write to use single activity both for list of data and for search results?</p>
  
  <p>So I get the query from intent, which comes from database class using LIKE query:</p>
  
  <pre><code>String sqlQuery1 = "SELECT * FROM city as t1, region as t2 WHERE t1.name LIKE" + "'%" + "?" + "%';";
  
           //Поиск запросом LIKE
        public Cursor fetchRecordsByQuery(String query) {
            return  myDataBase.rawQuery(sqlQuery1, new String[] {query});
        }
  </code></pre>
  
  <p>Please could you advise what I am doing wrong? As I have told the task is to realize the search by Android Widget in the directory of cities. I think the problem is with doMySearch(query); method. What way and where it will be right to put search results?</p>
  
  <p>Here is the code of MainActivity.java:</p>
  
  <pre><code>package com.example.citycode;
  
  import java.util.concurrent.TimeUnit;
  
  import android.app.SearchManager;
  import android.content.Context;
  import android.content.Intent;
  import android.database.Cursor;
  import android.os.Bundle;
  import android.support.v4.app.LoaderManager.LoaderCallbacks;
  import android.support.v4.content.CursorLoader;
  import android.support.v4.content.Loader;
  import android.support.v4.widget.SimpleCursorAdapter;
  import android.support.v7.app.ActionBarActivity;
  import android.support.v7.widget.SearchView;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.widget.ListView;
  
  public class MainActivity extends ActionBarActivity implements LoaderCallbacks&lt;Cursor&gt; {
  
    ListView lvData;
    DBHelper db;
    SimpleCursorAdapter scAdapter;
  
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      // Открываем подключение к БД
      db = new DBHelper(this);
      db.open();
  
      // Get the intent, verify the action and get the query
      Intent intent = getIntent();
      if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        doMySearch(query);
      }
  
  
  
      // Формируем столбцы сопоставления
      String[] from = new String[] { DBHelper.COLUMN_r_name, DBHelper.COLUMN_region, DBHelper.COLUMN_code};
      int[] to = new int[] { R.id.city_name, R.id.region_name, R.id.city_code };
  
      // Создаем адаптер и настраиваем список
      scAdapter = new SimpleCursorAdapter(this, R.layout.item, null, from, to, 0);
      lvData = (ListView) findViewById(R.id.listdata);
      lvData.setAdapter(scAdapter);
  
      // Создаем лоадер для чтения данных
      getSupportLoaderManager().initLoader(0, null, this);
  
  
    }
  
    protected void onDestroy() {
      super.onDestroy();
      // Закрываем подключение к БД при выходе
      db.close();
    }
  
    public void doMySearch(String query) {
          //Ищем совпадения
          Cursor cursor1 = db.fetchRecordsByQuery(query);
          startManagingCursor(cursor1);
          String[] from = new String[] { DBHelper.COLUMN_r_name, DBHelper.COLUMN_region, DBHelper.COLUMN_code};
          int[] to = new int[] { R.id.city_name, R.id.region_name, R.id.city_code };
  
          SimpleCursorAdapter records = new SimpleCursorAdapter(this,
                  R.layout.item, cursor1, from, to);
          //Обновляем адаптер
          lvData.setAdapter(records);
        }
  
       @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
  
          // Get the SearchView and set the searchable configuration
          SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
          SearchView searchView = (SearchView) menu.findItem(R.id.action_settings).getActionView();
          // Assumes current activity is the searchable activity
          //searchView.setSubmitButtonEnabled(true);
          searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
          searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
  
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
          if (id == R.id.action_settings) {
              //onSearchRequested();
              return true;
          }
          return super.onOptionsItemSelected(item);
      }
  
    @Override
    public Loader&lt;Cursor&gt; onCreateLoader(int id, Bundle bndl) {
      return new MyCursorLoader(this, db);
    }
  
    @Override
    public void onLoadFinished(Loader&lt;Cursor&gt; loader, Cursor cursor) {
      scAdapter.swapCursor(cursor);
    }
  
    @Override
    public void onLoaderReset(Loader&lt;Cursor&gt; loader) {
    }
  
    static class MyCursorLoader extends CursorLoader {
  
        DBHelper db;
  
      public MyCursorLoader(Context context, DBHelper db) {
        super(context);
        this.db = db;
      }
  
      @Override
      public Cursor loadInBackground() {
        Cursor cursor = db.getAllData();
        try {
          TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        return cursor;
      }
  
    }
  }
  </code></pre>
  
  <p>I got following error: </p>
  
  <pre><code>10-21 13:50:03.697: E/AndroidRuntime(12899): 
  java.lang.RuntimeException: 
  Unable to start activity ComponentInfo{com.example.citycode/com.example.citycode.MainActivity}: 
  java.lang.IllegalArgumentException: 
  Cannot bind argument at index 1 because the index is out of range. The statement has 0 parameters.
  </code></pre>
  
  <p>What could I be doing wrong?</p>
  
  <p>Here is the MainActivity.java code with more simple implementation, but still not working...</p>
  
  <pre><code>package com.example.citycode;
  
  import android.app.SearchManager;
  import android.content.Context;
  import android.content.Intent;
  import android.database.Cursor;
  import android.os.Bundle;
  import android.support.v7.app.ActionBarActivity;
  import android.support.v7.widget.SearchView;
  import android.view.Menu;
  import android.widget.ListView;
  import android.widget.SimpleCursorAdapter;
  
  public class MainActivity extends ActionBarActivity {
  
    ListView lvData;
    DBHelper db;
    SimpleCursorAdapter scAdapter;
    SimpleCursorAdapter records;
    Cursor cursor;
    Cursor cursor1;
  
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      // открываем подключение к БД
      db = new DBHelper(this);
      db.open();
  
      // Get the intent, verify the action and get the query
      Intent intent = getIntent();
      if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        doMySearch(query);
      }
  
      // получаем курсор
      cursor = db.getAllData();
      startManagingCursor(cursor);
  
      // Формируем столбцы сопоставления
      String[] from = new String[] { DBHelper.COLUMN_r_name, DBHelper.COLUMN_region, DBHelper.COLUMN_code};
      int[] to = new int[] { R.id.city_name, R.id.region_name, R.id.city_code };
  
      // создааем адаптер и настраиваем список
      scAdapter = new SimpleCursorAdapter(this, R.layout.item, cursor, from, to);
      lvData = (ListView) findViewById(R.id.listdata);
      lvData.setAdapter(scAdapter);
    }
  
    public void doMySearch(String query) {
          //Ищем совпадения
          cursor1 = db.fetchRecordsByQuery(query);
          startManagingCursor(cursor1);
          String[] from = new String[] { DBHelper.COLUMN_r_name, DBHelper.COLUMN_region, DBHelper.COLUMN_code};
          int[] to = new int[] { R.id.city_name, R.id.region_name, R.id.city_code };
  
          SimpleCursorAdapter records = new SimpleCursorAdapter(this,
                  R.layout.item, cursor1, from, to);
          //Обновляем адаптер
          lvData.setAdapter(records);
        }
  
  
  
       @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.options_main, menu);
  
          // Get the SearchView and set the searchable configuration
          SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
          SearchView searchView = (SearchView) menu.findItem(R.id.action_settings).getActionView();
          // Assumes current activity is the searchable activity
          //searchView.setSubmitButtonEnabled(true);
          searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
          searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
  
          return true;
      }
  
  
    protected void onDestroy() {
      super.onDestroy();
      // закрываем подключение при выходе
      db.close();
    }
  
  } 
  </code></pre>