如何在Android应用程序中检索搜索结果

时间:2015-04-10 16:43:35

标签: java android

我有一个Android应用程序,我想在其中搜索查询并在同一活动中检索结果。这是我的代码(来自http://developer.android.com/guide/topics/search/search-dialog.html):

searchable.xml

 <?xml version="1.0" encoding="utf-8"?>
  <searchable
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:label="@string/search_label"
     android:hint="@string/search_hint"
     android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
     android:searchSuggestAuthority="co.glurl.appoid.SearchSuggestionProvider"
    android:searchSuggestSelection=" ?" >
 </searchable>

SearchableActivity.java

public class SearchableActivity extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search);
        this.setDefaultKeyMode(Activity.DEFAULT_KEYS_SEARCH_LOCAL);
        final Intent queryIntent = getIntent();
        final String queryAction = queryIntent.getAction();
        if (Intent.ACTION_SEARCH.equals(queryAction)){
            this.doSearchQuery(queryIntent);
        }
        return;
    }
    @Override
    public void onNewIntent(final Intent newIntent){
        super.onNewIntent(newIntent);

        final Intent queryIntent = getIntent();
        final String queryAction = queryIntent.getAction();
        if (Intent.ACTION_SEARCH.equals(queryAction)){
            this.doSearchQuery(queryIntent);
        }
    }
    private void doSearchQuery(final Intent queryIntent){
        final String query = queryIntent.getStringExtra(SearchManager.QUERY);
        SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
                SearchSuggestionProvider.AUTHORITY,
                SearchSuggestionProvider.MODE);
        suggestions.saveRecentQuery(query, null);

        Log.i("YOU ENTERED", query );
    }
}

SearchSuggestionProvider.java

public class SearchSuggestionProvider extends SearchRecentSuggestionsProvider {
public final static String AUTHORITY = "co.glurl.appoid.SearchSuggestionProvider";
public final static int MODE = DATABASE_MODE_2LINES | DATABASE_MODE_QUERIES;
    public SearchSuggestionProvider() {
        super();
        setupSuggestions(AUTHORITY, MODE);
    }
}

的AndroidManifest.xml

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.Chilli" >
    <provider android:name=".SearchSuggestionProvider"
        android:authorities="co.glurl.appoid.SearchSuggestionProvider" />
    <activity android:name=".SplashScreen"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="text/plain" />
        </intent-filter>
    </activity>
    <activity android:name=".SearchableActivity"
        android:launchMode="singleTop" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data android:name="android.app.searchable"
            android:resource="@xml/searchable"/>
        <meta-data android:name="android.app.default_searchable"
            android:value=".SearchableActivity" />
    </activity>
</application>

显示搜索对话框但单击设备搜索按钮时未提供restults。有帮助吗?感谢。

0 个答案:

没有答案