为什么我的recyclerview不刷新

时间:2015-10-14 09:11:28

标签: java android

我有一个名为ListActivity的活动,其中包含一个回收者视图和一个Loader,我在此活动中有一个NavigationDrawer。现在当我按下NavigationDrawer中的一个项目时,我希望我的Recycler视图相应地刷新。

在我的onCreate:

mRecyclerView = (RecyclerView) findViewById(R.id.detailsList);

装载机:

 ListActivity.this.getLoaderManager().initLoader(0, null, new LoaderManager.LoaderCallbacks<Cursor>() {
            @Override
            public Loader<Cursor> onCreateLoader(int id, Bundle args) {
                return new CursorLoader(ListActivity.this, EmployeeContentProvider.URI_EMPLOYEES, Employee.FIELDS, null, null, Employee.COL_NAME);
            }

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

                if (mCur != null && mCur.moveToFirst()) {
                    do {

                        EmployeeItem empInstance = new EmployeeItem();
                        empInstance.setCall(mCur.getString(mCur.getColumnIndexOrThrow("mobile")));
                        empInstance.setEmail(mCur.getString(mCur.getColumnIndexOrThrow("email")));
                        empInstance.setEmpID(mCur.getString(mCur.getColumnIndexOrThrow("empid")));
                        empInstance.setImageURL(mCur.getString(mCur.getColumnIndexOrThrow("image")));
                        empInstance.setName(mCur.getString(mCur.getColumnIndexOrThrow("name")));
                        employeeList.add(empInstance);
                    } while (mCur.moveToNext());

                    adapter = new DetailsRecyclerAdapter(ListActivity.this, employeeList);
                    mRecyclerView.setAdapter(adapter);
                }
            }

            @Override
            public void onLoaderReset(Loader<Cursor> arg0) {

            }
        });

以上工作完美无瑕。现在在我的导航抽屉的onClick中:

@Override
    public void onDrawerItemSelected(View view, int position, String value) {
        Log.d("Department selected: ", value);

        final String selectionArgs = value.trim();

        ListActivity.this.getLoaderManager().restartLoader(0, null, new LoaderManager.LoaderCallbacks<Cursor>() {
            @Override
            public Loader<Cursor> onCreateLoader(int id, Bundle args) {

                return new CursorLoader(ListActivity.this, EmployeeContentProvider.URI_EMPLOYEES, Employee.FIELDS, Employee.COL_DEPARTMENT + " IS ? COLLATE NOCASE", new String[]{selectionArgs}, Employee.COL_NAME);
            }

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

                if (mCur != null && mCur.moveToFirst()) {
                    do {

                        EmployeeItem empInstance = new EmployeeItem();
                        empInstance.setCall(mCur.getString(mCur.getColumnIndexOrThrow("mobile")));
                        empInstance.setEmail(mCur.getString(mCur.getColumnIndexOrThrow("email")));
                        empInstance.setEmpID(mCur.getString(mCur.getColumnIndexOrThrow("empid")));
                        empInstance.setImageURL(mCur.getString(mCur.getColumnIndexOrThrow("image")));
                        empInstance.setName(mCur.getString(mCur.getColumnIndexOrThrow("name")));
                        employeeList.add(empInstance);
                    } while (mCur.moveToNext());

                    adapter = new DetailsRecyclerAdapter(ListActivity.this, employeeList);
                    mRecyclerView.setAdapter(adapter);

                    System.out.println("restarted loader");
                }
            }

            @Override
            public void onLoaderReset(Loader<Cursor> arg0) {
                System.out.println("reset loader");
            }
        });
    }

在上面我认为加载器正在执行,因为我得到System.out日志,但我的RecyclerView没有刷新。我在这里错过了什么?我的数据库与内容提供商桥接,我已正确设置选择和选择参数。

以下是我的ContentProvider的相关部分:

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

        System.out.println("Provider"+selection+selectionArgs);
        Cursor result = null;
        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;
    }

0 个答案:

没有答案