FilterQueryProvider无法正常工作

时间:2015-10-15 05:44:38

标签: android android-contentprovider

我正在使用以下代码段:

FilterQueryProvider provider = new FilterQueryProvider() {
                    @Override
                    public Cursor runQuery(CharSequence constraint) {
                       /* Uri uri = TextUtils.isEmpty(constraint) ? EmployeeContentProvider.URI_EMPLOYEES :
                                Uri.withAppendedPath(EmployeeContentProvider.URI_EMPLOYEES, constraint.toString()); */
                        return getContentResolver().query( EmployeeContentProvider.URI_EMPLOYEES, Employee.FIELDS, Employee.COL_NAME + " IS ?", new String[]{constraint.toString()}, null);

                    }
                };

在上面,如果我使用Uri.withAppendedPath并提供_id字段作为搜索constriant,它可以正常工作。但是我想根据名称进行搜索。这里不行。我的合作伙伴提供商如下:

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

        Cursor result = null;
        System.out.println("Selection: " + selection);
        System.out.println("Selection args: " + Arrays.toString(selectionArgs));

        if (URI_EMPLOYEES.equals(uri)) {
            result = DatabaseHandler
                    .getInstance(getContext())
                    .getReadableDatabase()
                    .query(Employee.TABLE_NAME, Employee.FIELDS, selection, selectionArgs, null, null, Employee.COL_NAME, null);

            result.setNotificationUri(getContext().getContentResolver(), URI_EMPLOYEES);
        } else if (uri.toString().startsWith(EMPLOYEE_BASE)) {

            final long id = Long.parseLong(uri.getLastPathSegment());
            result = DatabaseHandler
                    .getInstance(getContext())
                    .getReadableDatabase()
                    .query(Employee.TABLE_NAME, Employee.FIELDS,
                            Employee.COL_ID + " IS ?",
                            new String[]{String.valueOf(id)}, null, null,
                            null, null);
            result.setNotificationUri(getContext().getContentResolver(), URI_EMPLOYEES);
        } else {
            throw new UnsupportedOperationException("Not yet implemented");
        }

        return result;
    }

我在这里想念什么?

1 个答案:

答案 0 :(得分:0)

所以这里是解决方案,这里有趣的是AND如何优先于OR

        public void onLoadFinished(Loader<Cursor> loader, Cursor mCur) {


            FilterQueryProvider provider = new FilterQueryProvider() {
                @Override
                public Cursor runQuery(CharSequence constraint) {
                    Uri uri = TextUtils.isEmpty(constraint) ? EmployeeContentProvider.URI_EMPLOYEES :
                            Uri.withAppendedPath(EmployeeContentProvider.URI_EMPLOYEES, constraint.toString());

                    String where = Employee.COL_DEPARTMENT + " IS ?" + " AND (" + Employee.COL_NAME + " LIKE ? OR " + Employee.COL_EMPID + " LIKE ? )";
                    String selectionArgs[] = new String[]{String.valueOf(department), String.valueOf("%" + constraint + "%"), String.valueOf("%" + constraint + "%")};
                    return getContentResolver().query(uri, Employee.FIELDS, where, selectionArgs, null);

                }
            };

            adapter1 = new CursorAdapter(ListActivity.this, R.layout.details_list_row, mCur);
            adapter1.setFilterQueryProvider(provider);
            mRecyclerView.setAdapter(adapter1);

        }