Search code examples
androidlistviewandroid-contentproviderandroid-cursorloader

CursorLoader change cursor() doesn't refresh listview


I use approach resuest to server - listView - content provider in many projects and it works well. However I faced with really strange issue - notifyChange(uri, null) doesn't refresh data in listview automatically if I insert or delete data as well.

    {
     ListView listView = (ListView) findViewById(R.id.list);
     View header = View.inflate(this, R.layout.header, null);
     listView.addHeaderView(header);


     adapter = new Adapter(this, R.layout.item, null, true);
     listView.setAdapter(adapter);

     getSupportLoaderManager().restartLoader(0, null, this);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
           return new CursorLoader(this, Statuses.URI, null , null, null, null);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
        adapter.changeCursor(arg1);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> arg0) {
        adapter.changeCursor(null);
    }

// inset method of content provider
    long id = db.getWritableDatabase().insertWithOnConflict(table, null, values, SQLiteDatabase.CONFLICT_REPLACE);
    Uri newUri = Uri.withAppendedPath(uri, Long.toString(id));
    cr.notifyChange(newUri, null);
    return newUri;

//delete method of contetn provider
   int count = db.getWritableDatabase().delete(table, selection, selectionArgs);
    cr.notifyChange(uri, null);
    return count;

Do you any issues with this code?


Solution

  • Resolved. Forgot to add Uri before cursor has returned.