【问题标题】:What am I doing wrong in the implementation of the Android search?我在执行 Android 搜索时做错了什么?
【发布时间】:2014-10-21 14:12:16
【问题描述】:

请告诉代码有什么问题?任务是在目录中实现搜索。我在当前 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(query);方法。以什么方式以及在哪里放置搜索结果是正确的?

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

} 

【问题讨论】:

    标签: android sqlite search android-sqlite


    【解决方案1】:
    "... WHERE t1.name LIKE" + "'%" + "?" + "%';"
    

    这不是 SQL 参数。 Java的字符串拼接后的结果是:

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

    搜索包含问号的名称。

    字符串外必须有问号,在SQL中需要字符串拼接:

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

    【讨论】:

    • 我已经写了你建议的查询,但是我有这样的错误:10-22 11:42:38.015: E/AndroidRuntime(8312): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.citycode/com.example.citycode.MainActivity}: java.lang.NullPointerException 我认为问题不在于查询...可能是搜索实现错误?
    • 导致第一个错误的问题似乎已解决。现在您有一个新问题,您应该单独提出。
    • 我试过 cursor1.getCount() - 得到 42000,所以问题肯定是搜索实现......
    猜你喜欢
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    • 1970-01-01
    • 2021-04-22
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    • 2020-10-06
    相关资源
    最近更新 更多