2015-02-20 3 views
1

Я работал в приложении, которое использует SearchView Widget как ActionView в ActionBar.Как открыть новое мероприятие, чтобы показать результаты поиска?

Проблема возникает, когда я печатаю поиск и нажимаю кнопку поиска, он открывает ту же самую активность, что я хочу сделать, это открыть новое действие и показать результаты в ListView, как это можно зафиксировать?

Это мой AndroidManifest.xml файл:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="andres.hotelsoria" > 

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher_hotel" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <action android:name="android.intent.action.SEARCH" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
     <meta-data 
      android:name="android.app.searchable" 
      android:resource="@xml/searchable" /> 
    </activity> 
    <activity 
     android:name=".SearchableActivity" 
     android:label="@string/title_activity_searchable" > 
    </activity> 
</application> 

ответ

2

Вы можете начать новую деятельность, добавив OnQueryTextListener в SearchView.

final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() { 
       @Override 
       public boolean onQueryTextSubmit(String query) { 
        Intent intent = new Intent(getApplicationContext(), SearchableActivity.Class); 
        startActivity(intent); 
        return true; 
       } 

       @Override 
       public boolean onQueryTextChange(String newText) { 

        return true; 

       } 
      }; 
searchView.setOnQueryTextListener(queryTextListener); 
0

// Вы ВГА, чтобы начать новую деятельность, как этот SearchView.OnQueryTextListener textListener = новый SearchView.OnQueryTextListener() {

@Override 
    public boolean onQueryTextSubmit(String query) { 
     //use intent here to start new activity and pass "query" string. 
     return true; 
    } 

    @Override 
    public boolean onQueryTextChange(String newText) { 

     return true; 

    } 
}; 

searchView.setOnQueryTextListener(textListener); 
1

прочитал этот пост http://developer.android.com/guide/topics/search/index.html

, как указано в этом документе, выполните следующие действия

1) Создайте папку в res-> xml-> searchable.xml наклеить содержание, как упоминание в документации

2) Перейдите на AndroidManifest.xml и измените действие (где вы хотите доставить результат для поиска) к этому

<activity android:name=".SearchableActivity" > 
    <intent-filter> 
     <action android:name="android.intent.action.SEARCH" /> 
    </intent-filter> 
    <meta-data android:name="android.app.searchable" 
       android:resource="@xml/searchable"/> 
</activity> 

3) Объявить SearchView в menu.xml файл в

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

<item 
    android:id="@+id/mi_search" 
    android:title="@string/search" 
    android:orderInCategory="2" 
    android:icon="@drawable/searchicon" 
    app:showAsAction="collapseActionView|ifRoom" 
    app:actionViewClass="android.support.v7.widget.SearchView" /> 

4) в (меню меню onCreateOptionsMenu) сделать этот код

public boolean onCreateOptionsMenu(Menu menu) { 
// Inflate the options menu from XML 
MenuInflater inflater = getMenuInflater(); 
inflater.inflate(R.menu.options_menu, menu); 

// Get the SearchView and set the searchable configuration 
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView(); 
// Assumes current activity is the searchable activity 
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); 
searchView.setIconifiedByDefault(false); // Do not iconify the widget; 

5) в onOptionsItemSelected. -> выборки Id зрения поиска -> простая паста onSearchRequested() внутри блока

public boolean onSearchRequested() { 
return super.onSearchRequested(); 

}

6) регистрировать SearchView с onQueryTextListener и делать то, что вы хотите сделать http://developer.android.com/reference/android/widget/SearchView.OnQueryTextListener.html

Смежные вопросы