0

我正在使用SearchView,我已添加搜索功能(工作),現在我試圖添加自定義建議。提供程序被創建,但是當我鍵入某些查詢()從來沒有因爲某些原因而被調用。無法顯示我的自定義建議使用SearchView

我在哪裏錯了?

這裏我的清單:

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

    <uses-feature 
     android:name="android.hardware.sensor.accelerometer" 
     android:required="true" /> 

    <uses-feature 
     android:name="android.permission-group.MICROPHONE" 
     android:required="true"/> 

    <uses-feature android:name="android.hardware.camera" 
     android:required="true" /> 

<!-- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" 
     android:maxSdkVersion="18" />--> 

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

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

     <provider 
      android:name=".RecipeIngredientTabs.SuggestionProvider" 
      android:authorities="com.chefme.chefme.RecipeIngredientTabs.SuggestionProvider"> 
     </provider> 

     <activity 
      android:name=".Startup" 
      android:theme="@style/AppTheme.NoActionBar"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".RecipeIngredientTabs.Main" 
      android:label="@string/app_name" 
      android:launchMode="singleTop" 
      android:theme="@style/AppTheme.NoActionBar" > 
      <intent-filter> 
       <action android:name="android.intent.action.SEARCH" /> 
      </intent-filter> 
      <meta-data android:name="android.app.searchable" 
       android:resource="@xml/searchable"/> 
     </activity> 

     <activity 
      android:name=".RecipeStep" 
      android:label="@string/title_activity_recipe_steps" 
      android:launchMode="singleInstance" 
      android:theme="@style/AppTheme.NoActionBar" /> 
     <activity 
      android:name=".RecipePreview.RecipePreview" 
      android:label="@string/title_activity_recipe_preview" 
      android:launchMode="singleInstance" 
      android:theme="@style/AppTheme.NoActionBar" /> 
     <activity 
      android:name=".ShoppingList.ShoppingList" 
      android:label="@string/title_activity_shopping_list" 
      android:launchMode="singleInstance" 
      android:theme="@style/AppTheme.NoActionBar" /> 
     <activity 
      android:name=".Settings.Diets" 
      android:label="@string/title_activity_diets" 
      android:launchMode="singleInstance" 
      android:theme="@style/AppTheme.NoActionBar"/> 
     <activity 
      android:name=".Favorite.FavoriteActivity" 
      android:label="@string/title_activity_favorite" 
      android:launchMode="singleInstance" 
      android:theme="@style/AppTheme.NoActionBar"/> 
     <activity 
      android:name=".ownRecipesCamera.TakePicture" 
      android:label="@string/title_activity_take_picture" 
      android:launchMode="singleInstance" 
      android:theme="@style/AppTheme.NoActionBar"/> 
     <activity 
      android:name=".Settings.Credits" 
      android:label="@string/title_activity_credits" 
      android:launchMode="singleInstance" 
      android:theme="@style/AppTheme.NoActionBar"/> 
    </application> 

</manifest> 

這裏我提供:

package com.chefme.chefme.RecipeIngredientTabs; 

import android.content.ContentProvider; 
import android.content.ContentValues; 
import android.database.Cursor; 
import android.database.MatrixCursor; 
import android.net.Uri; 
import android.support.annotation.Nullable; 

import DBUtility.Data; 


public class SuggestionProvider extends ContentProvider{ 
    @Override 
    public boolean onCreate() { 
     System.out.println("Creation Provider"); 
     return true; 
    } 
@Nullable 
@Override 
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { 
    String query = uri.getLastPathSegment().toLowerCase(); //Dovrebbe essere chiamato tramite searchable.xml 
    System.out.println("write: "+ query); 

    String[] columns = new String[]{"_ID", "SUGGEST_COLUMN_TEXT_1", "SUGGEST_COLUMN_ICON_1", "SUGGEST_COLUMN_INTENT_DATA"}; 
    MatrixCursor cursor = new MatrixCursor(columns); 

    Object[] row = new Object[]{0, Data.currentIngredients.get(0).getName(), Data.currentIngredients.get(0).getImage(), "Gigi"}; 
    cursor.addRow(row); 

    return cursor; 
} 

@Nullable 
@Override 
public String getType(Uri uri) { 
    return null; 
} 

@Nullable 
@Override 
public Uri insert(Uri uri, ContentValues contentValues) { 
    return null; 
} 

@Override 
public int delete(Uri uri, String s, String[] strings) { 
    return 0; 
} 

@Override 
public int update(Uri uri, ContentValues contentValues, String s, String[] strings) { 
    return 0; 
} 

}

這裏我searchable.xml:

這裏我主要的:

package com.chefme.chefme.RecipeIngredientTabs; 

import android.Manifest; 
import android.app.Activity; 
import android.app.SearchManager; 
import android.content.Context; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.os.Bundle; 
import android.os.Environment; 
import android.support.design.widget.TabLayout; 
import android.support.v4.app.ActivityCompat; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.support.design.widget.NavigationView; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.widget.SearchView; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.Toast; 

import com.chefme.chefme.NavbarActivity; 
import com.chefme.chefme.R; 

import java.io.File; 

import DBUtility.Data; 

public class Main extends NavbarActivity { 

    private SectionsPagerAdapter selectorPagerAdapter; 
    private ViewPager mViewPager; 
    private TabLayout tabLayout; 
    private Toast backtoast; 

    // Storage Permissions 
    private static final int REQUEST_EXTERNAL_STORAGE = 1; 
    private static String[] PERMISSIONS_STORAGE = { 
      Manifest.permission.READ_EXTERNAL_STORAGE, 
      Manifest.permission.WRITE_EXTERNAL_STORAGE 
    }; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     super.replaceContentLayout(R.layout.main_content, super.CONTENT_LAYOUT_ID); 

     verifyDirectoryExists(); 

     verifyStoragePermissions(this); 

     handleIntent(getIntent()); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, //host activity 
       drawer, //drawerLayout object 
       toolbar, //nav drawer icon to replace 
       R.string.navigation_drawer_open, 
       R.string.navigation_drawer_close); 
     drawer.setDrawerListener(toggle); 
     toggle.syncState(); 

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 

     // Create the adapter that will return a fragment for each of the three 
     // primary sections of the activity. 
     selectorPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 

     // Set up the ViewPager with the sections adapter. 
     mViewPager = (ViewPager) findViewById(R.id.container); 
     mViewPager.setAdapter(selectorPagerAdapter); 

     tabLayout = (TabLayout) findViewById(R.id.tabs); 
     tabLayout.setupWithViewPager(mViewPager); 
    } 

    @Override 
    protected void onResume(){ 
     super.onResume(); 
     Data.active_main = true; 
    } 

    @Override 
    protected void onPause(){ 
     super.onPause(); 
     Data.active_main = false; 
    } 

    public void onBackPressed() { 
      if(backtoast!=null&&backtoast.getView().getWindowToken()!=null) { 
       finish(); 
      } else { 
       backtoast = Toast.makeText(this, "Press back to exit", Toast.LENGTH_SHORT); 
       backtoast.show(); 
      } 
    } 

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

    private void handleIntent(Intent intent) { 
     if (Intent.ACTION_SEARCH.equals(intent.getAction())) { 
      String query = intent.getStringExtra(SearchManager.QUERY); 
      System.out.println("Searched: " + query); 
     } 
     else if (Intent.ACTION_VIEW.equals(intent.getAction())) { //Intent partito da SuggestionProvider 
      String query = intent.getDataString(); 
      System.out.println("Suggested: " + query); 
     } 

    } 

    //------------------------------------------------------------------------------------------ 
    //        TAB SELECTOR FUNCTIONS 
    //------------------------------------------------------------------------------------------ 

    public class SectionsPagerAdapter extends FragmentPagerAdapter { 

     public SectionsPagerAdapter(FragmentManager fm) { 
      super(fm); 
     } 

     @Override 
     public Fragment getItem(int position) { 
      // getItem is called to instantiate the fragment for the given page. 
      if(position == 0) 
       return new TabFragmentIngredients(); 
      if(position == 1) 
       return new TabFragmentRecipes(); 
      else 
       return null; 
     } 

     @Override 
     public int getCount() { 
      // Show 2 total pages. 
      return 2; 
     } 


     @Override 
     public CharSequence getPageTitle(int position) { 
      switch (position) { 
       case 0: 
        return getString(R.string.name_ingredients_hint); 
       case 1: 
        return getString(R.string.name_recipes_hint); 
      } 
      return null; 
     } 
    } 


    //------------------------------------------------------------------------------------------ 
    //          MENU FUNCTIONS 
    //------------------------------------------------------------------------------------------ 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.main, 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 true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     switch (item.getItemId()) { 
      case R.id.action_settings: showOrderbyDialog(); 
       return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    private void showOrderbyDialog() { 
     FragmentManager fm = getSupportFragmentManager(); 
     OrderByDialog orderbyDialogDialog = new OrderByDialog(); 
     orderbyDialogDialog.show(fm, ""); 
    } 

    public static void verifyDirectoryExists(){ 

     String folder_main = "GnammyRecipes"; 

     File f = new File(Environment.getExternalStorageDirectory()+"/Pictures/", folder_main); 

     if (!f.exists()) { 
      f.mkdirs(); 
     } 
    } 


    public static void verifyStoragePermissions(Activity activity) { 
     // Check if we have write permission 
     int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE); 

     if (permission != PackageManager.PERMISSION_GRANTED) { 
      // We don't have permission so prompt the user 
      ActivityCompat.requestPermissions(
        activity, 
        PERMISSIONS_STORAGE, 
        REQUEST_EXTERNAL_STORAGE 
      ); 
     } 
    } 



} 

回答

0

能不能請你改變這個

android:searchSuggestAuthority="com.chefme.chefme.RecipeIngredientTabs.SuggestionProvider 

android:searchSuggestAuthority="@string/application_package_name" 
    android:searchSuggestSelection=" ?" 

而且你缺少的元數據標籤

<meta-data 
       android:name="android.app.default_searchable" 
       android:value="com.chefme.chefme.RecipeIngredientTabs.YOURSEARCHACTIVITY" > 
      </meta-data> 

請參考以下鏈接 https://developer.android.com/guide/topics/search/search-dialog.html

+0

我試過了,沒有改變。我已經一步一步地跟蹤了這個指南,看起來一切都是正確的。 – Marco

+0

它會在日誌中拋出任何錯誤嗎?請在您的搜索活動類中添加調試點以查看它是否達到tr –

+0

我得到的唯一錯誤是:「08-25 12:30:21.786 2264-2264/com.chefme.chefme E/SpannableStringBuilder:SPAN_EXCLUSIVE_EXCLUSIVE跨度不能有零長度「,但我不認爲與此有關,你認爲如何? – Marco

相關問題