我在Android搜索的实施中做错了什么?

时间:2014-10-21 14:12:16

标签: android sqlite search android-sqlite

请告诉代码有什么问题?任务是在目录中实现搜索。我在清单文件中提到了当前MainActivity.java中的SEARCH(它是一个可搜索的活动)。 是否写入将单个活动用于数据列表和搜索结果?

所以我从intent获得了查询,它来自使用LIKE查询的数据库类:

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});
      }

请你告诉我我做错了什么?正如我所说,任务是通过Android Widget在城市目录中实现搜索。我认为问题在于doMySearch(查询);方法。放置搜索结果的方式和位置是什么?

以下是MainActivity.java的代码:

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<Cursor> {

  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<Cursor> onCreateLoader(int id, Bundle bndl) {
    return new MyCursorLoader(this, db);
  }

  @Override
  public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    scAdapter.swapCursor(cursor);
  }

  @Override
  public void onLoaderReset(Loader<Cursor> 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;
    }

  }
}

我收到了以下错误:

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.

我可能做错了什么?

以下是MainActivity.java代码,实现更简单,但仍无法正常工作......

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();
  }

} 

1 个答案:

答案 0 :(得分:0)

"... WHERE t1.name LIKE" + "'%" + "?" + "%';"

这不是SQL参数。 在Java的字符串连接之后,结果是:

"... WHERE t1.name LIKE'%?%';"

搜索包含问号的名称。

您必须在字符串外部加上问号,这需要SQL中的字符串连接:

"... WHERE t1.name LIKE '%' || ? || '%';"