android-无法为searchview ContentProvider解析Uri CONTENT_URI

时间:2017-01-12 08:51:13

标签: android google-maps search uri android-contentprovider

我为我的地图构建了一个searchView,因此我创建了Locaionrovider类来扩展ContentProvider并创建了AUTHORITY& CONTENT_UR在里面。 在SearchableLocation Activity中我调用了CONTENT_UR,但是它给出了这个错误: 无法解析CONTENT_URI

我试图找出问题,但没有成功。

SearchableLocations

public class SearchableLocations extends Activity {
private TextView mTextView;

private ListView mListView;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search_main);
    mTextView = (TextView) findViewById(R.id.text);
    mListView = (ListView) findViewById(R.id.search_list);


    handleIntent(getIntent());


}


protected void onNewIntent (Intent intent){

    handleIntent(intent);

}

private void handleIntent(Intent intent) {
    if (Intent.ACTION_VIEW.equals(intent.getAction())) {
        Intent nameIntent= new Intent(this, NameActivity.class);
        nameIntent.setData(intent.getData());
        startActivity(nameIntent);
    }else if (Intent.ACTION_SEARCH.equals(intent.getAction())){
        String query = intent.getStringExtra(SearchManager.QUERY);
        showResults(query);
    }
}


@SuppressLint("StringFormatInvalid")
private void showResults (String query ){
    Cursor cursor= getContentResolver().query(LocationProvider.CONTENT_URI,
            null, null, new String[]{query}, null);

    if (cursor ==null){
        mTextView.setText(getString(R.string.noresult, new Object[]
        {query}));
    } else {
        int count = cursor.getCount();
        String countString = getResources().getQuantityString(
                R.plurals.search_results,count, new Object[] {count,query}
        );

        mTextView.setText(countString);

        String [] from = new String[] {
                LocationDatabase.KET_NAME,
               // LocationDatabase.KEY_LOCATION
        };

        int[] to = new int[]{
                R.id.name,
              //  R.id.location
        };

        SimpleCursorAdapter names = new SimpleCursorAdapter(
                this, R.layout.search_result, cursor, from, to
        );
        mListView.setAdapter(names);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                Intent nameIntent = new Intent(getApplicationContext(),
                    NameActivity.class    );

                Uri data = Uri.withAppendedPath(LocationProvider.CONTENT_URI,
                        String.valueOf(id));
                nameIntent.setData(data);
                startActivity(nameIntent);

            }
        });
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_menu, menu);

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false);
    }

    return true;
}

LocationProvider

 public class LocationProvider extends ContentProvider {

String TAG="LocationProvider";

public static String AUTHORITY="com.playpersia.bicyclemap.LocationProvider";
public static final Uri CONTENT_URI =Uri.parse("content://" +AUTHORITY + "location");

// MIME types used for searching words or looking up a single location

public static final String LOCATION_MIME_TYPE= ContentResolver.CURSOR_DIR_BASE_TYPE +
        "/vnd.com.playpersia.bicyclemap";

public static final String DEFINITION_MIME_TYPE =ContentResolver.CURSOR_DIR_BASE_TYPE +
        "/vnd.com.playpersia.bicyclemap";

private LocationDatabase mLocation;

//UriMatcher

private static final  int SEARCH_NAMES=0;
private static final  int GET_NAMES=1;
private static final  int SEARCH_SUGGEST=2;
private static final  int REFRESH_SHORTCUT=3;
private static final UriMatcher sURIMATCHER= buildUriMatcher();


private static UriMatcher buildUriMatcher(){
    UriMatcher matcher= new UriMatcher(UriMatcher.NO_MATCH);

    matcher.addURI(AUTHORITY , "location", SEARCH_NAMES);
    matcher.addURI(AUTHORITY, "location/#", GET_NAMES);

    matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY,SEARCH_SUGGEST);
    matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY +
            "/*", SEARCH_SUGGEST);



    matcher.addURI(AUTHORITY,SearchManager.SUGGEST_URI_PATH_SHORTCUT,REFRESH_SHORTCUT);
    matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_SHORTCUT
            +"/*", REFRESH_SHORTCUT);

    return matcher;

}
@Override
public boolean onCreate() {

    mLocation= new LocationDatabase(getContext());

    return true;
}

@Nullable
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    switch (sURIMATCHER.match(uri))
    {
        case  SEARCH_SUGGEST:
            if (selectionArgs == null){

                throw new IllegalArgumentException(
                        "selectionArgs nust be provided for the Uri:" + uri);
            }

            return getSuggestion(selectionArgs[0]);

        case SEARCH_NAMES:

            if (selectionArgs == null) {
                throw new IllegalArgumentException(
                        "selectionArgs must be provided for the Uri: " + uri);
            }

            return  search(selectionArgs[0]);
        case GET_NAMES:
            return  getName(uri);
        case  REFRESH_SHORTCUT:
            return  refreshShortcut(uri);
        default:
            throw new IllegalArgumentException("Unknown Uri: " + uri);

    }

}



private Cursor getSuggestion(String query) {

    query=query.toLowerCase();
    String[] columns = new String[]{
            BaseColumns._ID,
            LocationDatabase.KET_NAME,
            LocationDatabase.KEY_LOCATION,

            SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID

    };

    return mLocation.getNameMatches(query, columns);
}

private  Cursor search (String query){
    query=query.toLowerCase();
    String[] columns = new String[]{
            BaseColumns._ID,
            LocationDatabase.KET_NAME,
          LocationDatabase.KEY_LOCATION,
    };

    return mLocation.getNameMatches(query, columns);

1 个答案:

答案 0 :(得分:0)

您是否在Manifest中注册了内容提供商? 你应该得到类似的东西:

<provider android:name=".LocationProvider"
          android:authorities="${applicationId}.LocationProvider"
          android:exported="false"
          android:label="@string/provider_locations" />

如果需要,可以使用您的应用相应地更改nameauthorities属性