2016-04-17 6 views
1

SearchViewの使用に問題があります。 「虫眼鏡」をタップして、活動を開き、ツールバーから検索を実行したいと思います。SearchViewにカスタム候補が表示されない

たManifest.xml

<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"/> 
</activity> 

<provider 
    android:authorities="com.example.provider" 
    android:name="com.example.MyCustomSearchSuggestions"> 
</provider> 

SearchableActivity.java

public class SearchableActivity extends AppCompatActivity { 

    private static final String TAG = "SearchableActivity"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_searchable); 
     handleIntent(getIntent()); 
    } 

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

    private void handleIntent(Intent intent) { 
     Log.d(TAG, "handleIntent: "); 
     if (Intent.ACTION_SEARCH.equals(intent.getAction())) { 
      Log.d(TAG, "handleIntent: ACTION_SEARCH"); 
      String searchQuery = intent.getStringExtra(SearchManager.QUERY); 

     } else if (Intent.ACTION_VIEW.equals(intent.getAction())) { 
      Log.d(TAG, "handleIntent: ACTION_VIEW"); 
      Uri detailUri = intent.getData(); 
      long id = Long.parseLong(detailUri.getLastPathSegment()); 
      finish(); 
     } else { 
      Log.d(TAG, "handleIntent: " + intent.getAction()); 
     } 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     // Inflate the options menu from XML 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.menu_profile, menu); 

     // Get the SearchView and set the searchable configuration 
     SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
     SearchView searchView = (SearchView) menu.findItem(R.id.search_profile).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 true; 
    } 

} 

:残念ながら、提案は私が...

を入力するとこれは私が持っているものである現れませんsearchable.xml

<?xml version="1.0" encoding="utf-8"?> 
<searchable xmlns:android="http://schemas.android.com/apk/res/android" 
    android:label="Search" 
    android:hint="Search user" 
    android:searchSuggestAuthority="com.example.provider" 
    android:searchSuggestIntentAction="android.intent.action.VIEW" > 
</searchable> 

menu_profile.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="com.example.android.ProfileActivity"> 
    <item 
     android:id="@+id/action_settings" 
     android:orderInCategory="100" 
     android:title="@string/action_settings" 
     app:showAsAction="never" /> 
    <item android:id="@+id/search_profile" 
     android:title="Szukaj" 
     android:icon="@drawable/ic_search" 
     app:showAsAction="collapseActionView|ifRoom" 
     app:actionViewClass="android.support.v7.widget.SearchView" /> 
</menu> 

MyCustomSearchSuggestions.java - 提案

public class MyCustomSearchSuggestions extends ContentProvider { 

    private static final String TAG = "MyCustomSearchSuggestio"; 

    @Override 
    public boolean onCreate() { 
     Log.d(TAG, "onCreate: "); 

     return false; 
    } 

    @Override 
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { 

     Log.d(TAG, "query: "); 

     String[] columns = {"_ID", SearchManager.SUGGEST_COLUMN_TEXT_1}; 
     MatrixCursor matrixCursor = new MatrixCursor(columns); 

     matrixCursor.addRow(new Object[] {1, "test"}); 
     matrixCursor.addRow(new Object[] {2, "test1"}); 
     return matrixCursor; 
    } 
} 
+0

あなたは単に 'setSuggestionsAdapter'を使用しようとしましたか?検索エンジン、コンテンツプロバイダなどはありません – pskink

+0

私は本当にこれを使用したいと思っていました。なぜなら、私はグローバル検索機能を使いたいからです。実際、長い研究の後、私は問題を見つけました。 – SmiglowiecX

答えて

0

のためのコンテンツプロバイダは、私は私の問題thereの解決策を発見しました。

なぜ問題が発生したのかわかりませんが、検索可能な設定でandroid:labelandroid:hintが原因でした。私はstringsを入れましたが、(documentationにも記載されています)であると考えられます。

関連する問題