2013-07-22 112 views
0

美好的一天! 他做了一切article from google,但用ActionbarSherlock和它的實現SearchView,但請求沒有發送到SearchActivity。 你們中的任何人都可以知道可能是什麼問題。只有當您按下鍵盤上的「enter」按鈕纔會發送請求,並且我希望使其工作類似於「實時搜索」,一旦開始進入,立即開始搜索。在MainFragmentActivity如何使用ActionBarSherlock進行搜索對話框(不搜索)

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?> 
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
       package="ru.ex.Interface" 
       android:versionCode="45" 
       android:versionName="2.2.1b" 
       android:installLocation="auto" 
      > 

     <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15"/> 

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




     <application 
       android:label="@string/app_name" 
       android:icon="@drawable/ic_launcher" 
       android:hardwareAccelerated="true" 
       android:theme="@style/Theme.Sherlock.Light.Custom" 

       > 


      <!-- enable the search dialog to send searches to SearchableActivity --> 
      <meta-data android:name="android.app.default_searchable" 
         android:value=".SearchActivity"/> 

      <activity 
        android:name=".SearchActivity" 
        android:screenOrientation="portrait" 
        android:parentActivityName=".MainFragmentActivity" 
        android:launchMode="singleTop" 
        > 
       <!-- This intent-filter identifies this activity as "searchable" --> 

       <intent-filter> 
        <action android:name="android.intent.action.SEARCH" /> 

        <category android:name="android.intent.category.DEFAULT" /> 
       </intent-filter> 

       <!-- This metadata entry provides further configuration details for searches --> 
       <!-- that are handled by this activity. --> 

       <meta-data 
         android:name="android.app.searchable" 
         android:resource="@xml/search_config" /> 

      </activity> 

      <activity 
        android:name=".MainFragmentActivity" 
        android:screenOrientation="portrait" 
        android:launchMode="singleTask" 
        > 

       <intent-filter> 
        <action android:name="android.intent.action.MAIN"/> 
        <category android:name="android.intent.category.LAUNCHER"/> 
       </intent-filter> 

      </activity> 

     </application> 


    </manifest> 

裝箱菜單

@Override 
    public boolean onCreateOptionsMenu(Menu menu) 
    { 

     getSupportMenuInflater().inflate(R.menu.serch_menu, menu);    
     SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
     SearchView searchView = (SearchView) menu.findItem(R.id.search_menu).getActionView();    
     searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));    
     searchView.setIconifiedByDefault(false); 
     return true; 
    } 

SearchActivity.java

public class SearchActivity extends CustomMenuFragmentActivity 
{ 


    @Override 
    public void onCreate(Bundle save) 
    { 
     super.onCreate(save); 
     setContentView(R.layout.search_page); 
     handleIntent(getIntent()); 
    } 

    private void handleIntent(Intent intent) 
    { 
     if (Intent.ACTION_SEARCH.equals(intent.getAction())) 
     { 
      String query = intent.getStringExtra(SearchManager.QUERY); 
      goSearch(query); 
     } 
    } 


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

    private void goSearch(final String s) 
    {} 
} 

回答

0

要連續搜索用戶類型,請設置查詢文本偵聽器。

SearchView.OnQueryTextListener queryTextListener =新SearchView.OnQueryTextListener() { 公共布爾onQueryTextChange(字符串newText) { goSearch(newText); 返回true; }

public boolean onQueryTextSubmit(String query) 
    { 
     goSearch(query); 
     return true; 
    } 
}; 
searchView.setOnQueryTextListener(queryTextListener); 

但是,它看起來像你的應用程序沒有設立這種連續搜索的。您在MainActivity的菜單中有SearchView,但啓動SearchActivity以顯示結果。相反,您的SearchView菜單項需要處於顯示結果的相同活動中,並且當找到新結果時,goSearch()方法應該更新活動的內容。

0

這是一個項目,我最近寫具有ListView旁邊ActionBarSherlock的搜索功能的代碼片段:

// Create a new search view on the ActionBar 
SearchView searchView = new SearchView(getSherlockActivity().getSupportActionBar().getThemedContext()); 
// Set the hint inside the search box 
searchView.setQueryHint("Search..."); 
// When the user inputs text, filter the FAQs 
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { 
    public boolean onQueryTextChange(String newText) { 
     // adapter.getFilter().filter(newText); 
     return true; 
    } 

    public boolean onQueryTextSubmit(String query) { 
     // adapter.getFilter().filter(query); 
     return true; 
    } 
}); 
// Create a "search" menu option to search the list 
MenuItem item = menu.add(Menu.NONE, 0, Menu.NONE, R.string.search).setIcon(R.drawable.ic_action_search).setActionView(searchView); 
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW); 

只需更換與任何你需要的評論adapter.getFilter()