操作栏搜索无法正常工作

时间:2015-11-21 13:10:12

标签: android android-actionbar

我尝试在我的应用程序中包含搜索操作栏,但它无效。

的manifest.xml

<activity
        android:name=".activity.SearchResultsActivity"
        android:parentActivityName="activity.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
    </activity>

xml文件夹中的searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:hint="search..."
    android:label="@string/app_name">
</searchable>

menu.xml文件

 <menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:appcompat="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/tools">

    <item
        android:id="@+id/menu_search"
        android:title="search"
        appcompat:actionViewClass="android.support.v7.widget.SearchView"
        appcompat:showAsAction="always"/>

</menu>

SearchResultsActivity.java

    public class SearchResultsActivity extends ActionBarActivity {
    DatabaseConnector dbConnector;
    CustomSearchList adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_act_result);

        handleIntent(getIntent());

        dbConnector = new DatabaseConnector();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_main, menu);
        SearchManager searchManager =
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView =
                (SearchView) menu.findItem(R.id.menu_search).getActionView();
        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));
        return true;
    }

    @Override
    protected void onNewIntent(Intent intent) {
        setIntent(intent);
        handleIntent(intent);
    }

    private void handleIntent(Intent intent) {

        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);

            Toast.makeText(getApplication(),query,Toast.LENGTH_SHORT).show();

          //  doMySearch(query);
        }
    }

    private void doMySearch(String query) {
        ListView lstResult=(ListView)findViewById(R.id.lstVwResult);
        ArrayList<String> lv = dbConnector.SearchResult(query);
        adapter = new CustomSearchList(SearchResultsActivity.this,lv);
        lstResult.setAdapter(adapter);
    }

}

问题
     *当我在搜索中输入时没有发生任何事情      * Toast没有我想要的Toast查询。
     *我也知道用于从SQLite DB搜索的 doMySearch(查询)是否正确。

SearchResult(查询)方法

    public ArrayList<String> SearchResult(String params){
  Cursor cr =   database.rawQuery("SELECT * FROM " + TABLE_CUS +
                    " LEFT JOIN " + TABLE_BOX + " ON " + TABLE_CUS + "." +       CONNO + " = " +
                    TABLE_BOX + "." + CONNO + " WHERE " + TABLE_CUS + "." + CONNO + " = ?",
            new String[]{params});
    ArrayList<String> ar = new ArrayList<>();
    cr.moveToNext();
    while (cr.isAfterLast()){
            ar.add(cr.getString(cr.getColumnIndex(CONNO)));
        cr.moveToNext();
    }
    return  ar;
}

0 个答案:

没有答案