2014-03-31 49 views
2

我想在我的應用程序中的操作欄中實現一個搜索小部件,但每次鍵入搜索查詢並按回車時,都不會有任何反應。我認爲這與我的AndroidManifest文件有關,但我嘗試了很多東西,但無法弄清楚。該查詢從api獲取結果。我一直試圖讓這個工作大約6個小時。這裏是我的代碼現在(我已經嘗試了很多不同的東西):沒有檢測到查詢輸入的操作欄搜索小部件

searchable.xml

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

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?> 

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

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 

    <meta-data android:name="android.app.default_searchable" 
     android:value=".SearchAppsActivity" /> 

    <activity 
     android:name="com.android.devon.BrowseAppsActivity" 
     android:label="@string/title_activity_browse_apps" > 

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

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

SearchAppsActivity

public class SearchAppsActivity extends ListActivity 
{ 
private AppFrenzy restClient; 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_browse_apps); 
    handleIntent(getIntent()); 

} 

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

public void onListItemClick(ListView l, View v, int position, long id) 
{ 

} 

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

} 

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

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

    return super.onCreateOptionsMenu(menu); 

} 

private void doSearch(String queryStr) 
{ 
    restClient = AppService.getService(); 

    restClient.searchApps(queryStr, new Callback<List<App>>() { 
     @Override 
     public void success(List<App> apps, Response response) { 
      setListAdapter(new AppListAdapter(SearchAppsActivity.this, apps)); 

      Log.v("test", "search successful"); 
      Log.v("test", "search results:" + apps.toString()); 
     } 

     @Override 
     public void failure(RetrofitError retrofitError) { 
      Log.v("test", "failure"); 
     } 
    }); 
} 
} 

任何想法,爲什麼我的搜索沒有被提交的?

回答

0

您可以針對您的問題使用SearchView.OnQueryTextListener。

例如,

main.xml中

   <item 
    android:id="@+id/action_websearch" 
    android:actionViewClass="android.widget.SearchView" 
    android:icon="@android:drawable/ic_menu_search" 
    android:showAsAction="always" 
    android:title="@string/search_title"/> 

在onCreateOptionsMenu()

 @Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.main, menu); 
    SearchView searchView = (SearchView) menu.findItem(R.id.action_websearch) 
      .getActionView(); 
    // searchView.setQueryHint("Search Patient name"); 
    searchView.setQueryHint(Html.fromHtml("<font color = #ffffff>" 
      + getResources().getString(R.string.hint) + "</font>")); 
    if (null != searchView) { 
     searchView.setSearchableInfo(searchManager 
       .getSearchableInfo(getComponentName())); 

     searchView.setIconifiedByDefault(true); 

    } 
     SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() { 


     public boolean onQueryTextChange(String newText) { 
      // this is your adapter that will be filtered 

     }  //this method will call while press (click) search button. 
       public boolean onQueryTextSubmit(String query) { 


       // add searching stuff here 

      } 
     } 
    return super.onCreateOptionsMenu(menu); 

    }  

注:

Don't need searchable.xml and any permission on androidmanifest.xml. 
+1

我想讓它使用searchable.xml工作 – theDazzler

相關問題