gpt4 book ai didi

android - UriMatcher 不会匹配 uri

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:14:28 25 4
gpt4 key购买 nike

我正在尝试为搜索对话框提供自定义建议。我正在使用 urimatcher 来匹配 uri。但它不起作用。我总是得到异常“java.lang.IllegalArgumentException: Unknown Uri: content://com.simple.search.SuggestionProvider/search_suggest_query/?limit=50”。请向我解释一下。我该怎么做才能解决这个问题?

private static final UriMatcher sURIMatcher = makeUriMatcher();
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// Use UriMatcher, to find out what type of request received. Next, form
// the corresponding query to the database
switch (sURIMatcher.match(uri)) {
case SEARCH_SUGGEST:
if (selectionArgs == null) {
throw new IllegalArgumentException(
"selectionArgs must be provided for the Uri: " + uri);
}
return getSuggestions(selectionArgs[0]);
case SEARCH_TESTS:
if (selectionArgs == null) {
throw new IllegalArgumentException(
"selectionArgs must be provided for the Uri: " + uri);
}
return search(selectionArgs[0]);
case GET_TEST:
return getRecord(uri);
default:
throw new IllegalArgumentException("Unknown Uri: " + uri);
}

制作Urimatcher

private static UriMatcher makeUriMatcher() {

UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
// For the record
matcher.addURI(AUTHORITY, "tests", SEARCH_TESTS);
matcher.addURI(AUTHORITY, "tests/#", GET_TEST);
// For suggestions table
matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY,
SEARCH_SUGGEST);
matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*",
SEARCH_SUGGEST);
return matcher;
}

日志

11-30 14:16:27.295: I/ActivityThread(1638): Publishing provider com.simple.search.SuggestionProvider: com.simple.search.SuggestionProvider
11-30 14:16:35.424: D/com.simple.search.com.simple.search.SuggestionProvider(1638): Unknown Uri: content://com.simple.search.SuggestionProvider/search_suggest_query/?limit=50
11-30 14:16:35.424: E/DatabaseUtils(1638): Writing exception to parcel
11-30 14:16:35.424: E/DatabaseUtils(1638): java.lang.IllegalArgumentException: Unknown Uri: content://com.simple.search.SuggestionProvider/search_suggest_query/?limit=50
11-30 14:16:35.424: E/DatabaseUtils(1638): at com.simple.search.SuggestionProvider.query(SuggestionProvider.java:122)
11-30 14:16:35.424: E/DatabaseUtils(1638): at android.content.ContentProvider$Transport.bulkQuery(ContentProvider.java:117)
11-30 14:16:35.424: E/DatabaseUtils(1638): at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:98)
11-30 14:16:35.424: E/DatabaseUtils(1638): at android.os.Binder.execTransact(Binder.java:287)
11-30 14:16:35.424: E/DatabaseUtils(1638): at dalvik.system.NativeStart.run(Native Method)

searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/search_hint"
android:label="@string/app_label"
android:searchSuggestAuthority="com.simple.search.SuggestionProvider"
android:searchSuggestIntentAction="android.intent.action.VIEW"
android:searchSuggestIntentData="content://com.simple.search.SuggestionProvider/tests"
/>

list

....
....
<activity
android:label="@string/app_name"
android:name=".SimpleSearch" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
....
....

最佳答案

为避免讨论,如果您提供更多信息,我将更改此答案...

但是现在...

你在 xml 中有 android:searchSuggestIntentData="content://com.simple.search.SuggestionProvider/tests"

所以你必须改变

private static UriMatcher makeUriMatcher() {

UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
// For the record
matcher.addURI(AUTHORITY, "tests", SEARCH_TESTS);
matcher.addURI(AUTHORITY, "tests/#", GET_TEST);
// For suggestions table
matcher.addURI(AUTHORITY, "tests/" + SearchManager.SUGGEST_URI_PATH_QUERY,
SEARCH_SUGGEST);
matcher.addURI(AUTHORITY, "tests/" + SearchManager.SUGGEST_URI_PATH_QUERY + "/*",
SEARCH_SUGGEST);
return matcher;
}

如果你没有看到差异,我添加了“tests/”

所以现在它将匹配 content://com.simple.search.SuggestionProvider/tests/search_suggest_query?limit=50 这正是 QSB 将发送的...

无论如何你可以/应该为你的查询添加限制

case SEARCH_SUGGEST:
if (selectionArgs == null) {
throw new IllegalArgumentException(
"selectionArgs must be provided for the Uri: " + uri);
}
final String limit = uri.getQueryParameter(SearchManager.SUGGEST_PARAMETER_LIMIT);
return getSuggestions(selectionArgs[0], limit);

然后在getSuggestions中

helper.getReadableDatabase().query(table, projection,
selection, selectionArgs, null, null, sortOrder, limit);

编辑:

AUTHORITY + "tests/"+ SearchManager.SUGGEST_URI_PATH_QUERY 应该和 android:searchSuggestIntentData 一样!!!

EDIT2: 来自文档 http://developer.android.com/guide/topics/search/adding-custom-suggestions.html

selection The value provided in the android:searchSuggestSelection attribute of your searchable configuration file, or null if you have not declared the android:searchSuggestSelection attribute. More about using this to get the query below. selectionArgs Contains the search query as the first (and only) element of the array if you have declared the android:searchSuggestSelection attribute in your searchable configuration. If you have not declared android:searchSuggestSelection, then this parameter is null. More about using this to get the query below.

添加 android:searchSuggestSelection="? "

关于android - UriMatcher 不会匹配 uri,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8394119/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com