1

我想通过内容提供者从我的SQLite 数据库中获取建议。实际上我能够读取数据库。但如果我不提交查询,建议不会出现。我想在搜索查询更改时接受建议。每次文本更改或单击以搜索小部件(我正在使用模拟器)时,我都会在 LogCat 中收到以下消息。我认为提供程序中的urimatcher存在问题,但我无法解决。谢谢。

这是我的 Logcat 结果

04-29 22:56:44.215: W/SuggestionsAdapter(26850): Search suggestions query threw an     exception.
04-29 22:56:44.215: W/SuggestionsAdapter(26850): java.lang.IllegalArgumentException: Unknown URI content://com.example.search_deneme.SuggestionProvider/search_suggest_query?limit=50
04-29 22:56:44.215: W/SuggestionsAdapter(26850):    at com.example.search_deneme.SuggestionProvider.query(SuggestionProvider.java:80)
04-29 22:56:44.215: W/SuggestionsAdapter(26850):    at android.content.ContentProvider$Transport.query(ContentProvider.java:178)
04-29 22:56:44.215: W/SuggestionsAdapter(26850):    at android.content.ContentResolver.query(ContentResolver.java:310)
04-29 22:56:44.215: W/SuggestionsAdapter(26850):    at android.app.SearchManager.getSuggestions(SearchManager.java:808)
04-29 22:56:44.215: W/SuggestionsAdapter(26850):    at android.widget.SuggestionsAdapter.runQueryOnBackgroundThread(SuggestionsAdapter.java:190)
04-29 22:56:44.215: W/SuggestionsAdapter(26850):    at android.widget.CursorFilter.performFiltering(CursorFilter.java:49)
04-29 22:56:44.215: W/SuggestionsAdapter(26850):    at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
04-29 22:56:44.215: W/SuggestionsAdapter(26850):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-29 22:56:44.215: W/SuggestionsAdapter(26850):    at android.os.Looper.loop(Looper.java:137)
04-29 22:56:44.215: W/SuggestionsAdapter(26850):    at android.os.HandlerThread.run(HandlerThread.java:60)

这是我的内容提供商。

public class SuggestionProvider extends ContentProvider {

DatabaseHandler mOpenHelper;
private SQLiteDatabase db;
private String dbName = "dbS";

public static final int A = 1;
public static final int B = 2;
public static final int C = 3;
public static final String tablePath = "titles";

private static final String BASE_PATH = "titles";
public static final String AUTHORITY = "com.example.search_deneme.SuggestionProvider";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
        + "/" + tablePath);


// public static final int MODE = DATABASE_MODE_QUERIES;

private static final String[] COLUMNS = {
        "_id", // must include this column
        SearchManager.SUGGEST_COLUMN_TEXT_1,
        SearchManager.SUGGEST_COLUMN_TEXT_2, };

@Override
public boolean onCreate() {
    // TODO Auto-generated method stub
    mOpenHelper = new DatabaseHandler(getContext(), dbName);

    return true;

}

private static final UriMatcher sURIMatcher = new UriMatcher(
        UriMatcher.NO_MATCH);
static {
    sURIMatcher.addURI(AUTHORITY, BASE_PATH, A);
    sURIMatcher.addURI(AUTHORITY, BASE_PATH + "/#", B);
    //sURIMatcher.addURI(AUTHORITY, BASE_PATH + "/*", C);
    sURIMatcher.addURI(AUTHORITY, "titles/" + SearchManager.SUGGEST_URI_PATH_QUERY,
            C);
    //sURIMatcher.addURI(AUTHORITY, "titles/" + SearchManager.SUGGEST_URI_PATH_QUERY + "/*",
          //  C);


}

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

    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
    queryBuilder.setTables(tablePath);
    int uriType = sURIMatcher.match(uri);
    switch (uriType) {
    case B:
        queryBuilder.appendWhere(mOpenHelper._ID + "="
                + uri.getLastPathSegment());
        break;
    case A:// no filter

        break;
    case C:
        queryBuilder.appendWhere(mOpenHelper._TITLENAME+ "="
                + uri.getLastPathSegment());

        break;
    default:
        throw new IllegalArgumentException("Unknown URI " + uri);

    }
    Cursor cursor = queryBuilder.query(mOpenHelper.getReadableDatabase(),
            projection, selection, selectionArgs, null, null, sortOrder);
    cursor.setNotificationUri(getContext().getContentResolver(), uri);
    return cursor;

    // TODO Auto-generated method stub

}


@Override
public String getType(Uri uri) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public Uri insert(Uri uri, ContentValues values) {
    // TODO Auto-generated method stub

    db = mOpenHelper.getWritableDatabase();
    return null;
}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public int update(Uri uri, ContentValues values, String selection,
        String[] selectionArgs) {
    // TODO Auto-generated method stub
    return 0;
}

}

这是我的活动。

public class SearchableActivity extends Activity implements OnQueryTextListener {

DatabaseHandler db;
String query2;
Uri mNewUri;

ArrayList<SuggestList> incomingList = new ArrayList<SuggestList>();

ContentValues mNewValues = new ContentValues();
CursorAdapter mCursorAdapter;

SuggestListAdapter deneme = null;



TextView titleid;
TextView titlename;
ListView suggestList;

String COLUMN_TITLEID = "titleid";
String COLUMN_TITLENAME = "titlename";

public static final String tablePath = "titles";
public static final String AUTHORITY = "com.example.search_deneme.SuggestionProvider";

public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
        + "/" + tablePath);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.searchlist);
    ab = R.id.titleID;
    ac = R.id.titleNAME;


    titleid = (TextView) findViewById(R.id.titleID);
    titlename = (TextView) findViewById(R.id.titleNAME);


    db = new DatabaseHandler(this, "dbS");
    suggestList = (ListView) findViewById(R.id.suggestList);

    deneme = new SuggestListAdapter(SearchableActivity.this, getApplication(), incomingList);



    // Get the intent, verify the action and get the query
    Intent intent = getIntent();
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        query2 = intent.getStringExtra(SearchManager.QUERY);
        handleIntent(getIntent());
        System.out.println(query2);
        // doMySearch(query);
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.search, menu);

    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(true); // Do not iconify the widget;
                                            // expand it by default
    searchView.setQueryRefinementEnabled(true);


    searchView.setOnQueryTextListener(new OnQueryTextListener() {

        @Override
        public boolean onQueryTextSubmit(String query) {
            // TODO Auto-generated method stub
            return false;
        };

        @Override
        public boolean onQueryTextChange(String newText) {
            // TODO Auto-generated method stub
            //deneme = null;
            //suggestList.setAdapter(null);
            handleIntent(getIntent());
            return true;
    };


});
    return true;
}

public boolean onSearchRequested() {
    Toast.makeText(SearchableActivity.this, "deneme", Toast.LENGTH_LONG)
            .show();
    return super.onSearchRequested();
}

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


private void handleIntent(Intent intent) {

    incomingList = new ArrayList<SuggestList>();

    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        query2 = intent.getStringExtra(SearchManager.QUERY);

        String[] mProjection = {
                // CONTENT_URI.getQueryParameter("_ID"),
                // CONTENT_URI.getQueryParameter("_TITLEID"),
                // CONTENT_URI.getQueryParameter("_TITLENAME")
                DatabaseHandler._ID, DatabaseHandler._TITLEID,
                DatabaseHandler._TITLENAME };


        String mSelectionClause = DatabaseHandler._TITLENAME + " LIKE ? ";
        String[] mSelectionArgs = { query2 + "%" };




        Cursor a = getContentResolver().query(
                SuggestionProvider.CONTENT_URI, mProjection,
                mSelectionClause, mSelectionArgs, "_id" + " limit 10 offset 20");
        // Cursor c = db.getWordMatches(query, null);


        System.out.println(a.getCount());

        a.moveToFirst();



        while (a.isAfterLast() == false) 
        {
            String a1 = a.getString(1);
            String a2 = a.getString(2);

            System.out.println("asda");
            SuggestList sg = new SuggestList(a1, a2);
            incomingList.add(sg);
            a.moveToNext();
        }

        a.close();


        deneme = new SuggestListAdapter(SearchableActivity.this, getApplication(), incomingList);

        suggestList.setAdapter(deneme);
        // doMySearch(query);
    }
}

@Override
public boolean onQueryTextSubmit(String query) {
    // TODO Auto-generated method stub
    System.out.println("123");
    return false;
}

@Override
public boolean onQueryTextChange(String newText) {
    // TODO Auto-generated method stub
    //suggestList.setAdapter(null);
    System.out.println("asdfghjk");
    //handleIntent(getIntent());

    return true;
}

}

最后这是我的 searchable.xml

    <?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label= "@string/ara"
    android:hint="@string/hint"
    android:searchSuggestAuthority="com.example.search_deneme.SuggestionProvider"
    android:searchSuggestThreshold="3" 
    android:searchSuggestSelection=" ? "
    android:searchSuggestIntentData="content://com.example.search_deneme.SuggestionProvider/titles/">
</searchable>
4

1 回答 1

0

如果您还没有这样做,您可能需要在清单中添加一些信息,告诉 Android 哪个 Activity 将处理搜索建议查询。例如...

<activity
        android:name=".YourActivityWhereTheUserEntersTheirQuery"
        android:label="@string/app_name" >
        <meta-data
            android:name="android.app.default_searchable"
            android:value=".YourActivityThatPerformsTheSearchAndShowsTheResults" />
    </activity>
于 2014-11-24T14:03:48.543 回答