Example usage for android.content ContentResolver delete

List of usage examples for android.content ContentResolver delete

Introduction

In this page you can find the example usage for android.content ContentResolver delete.

Prototype

public final int delete(@RequiresPermission.Write @NonNull Uri url, @Nullable String where,
        @Nullable String[] selectionArgs) 

Source Link

Document

Deletes row(s) specified by a content URI.

Usage

From source file:edu.mit.mobile.android.locast.casts.CastDetail.java

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DIALOG_CONFIRM_DELETE:
        return new AlertDialog.Builder(this)
                .setPositiveButton(R.string.dialog_button_delete, new DialogInterface.OnClickListener() {

                    @Override//from w  ww.j a va2  s.c om
                    public void onClick(DialogInterface dialog, int which) {
                        final Uri cast = getIntent().getData();
                        final ContentResolver cr = getContentResolver();
                        cr.delete(Cast.getCastMediaUri(cast), null, null);
                        final int count = cr.delete(cast, null, null);
                        if (Constants.DEBUG) {
                            if (count != 1) {
                                Log.w(TAG, "got non-1 count from delete()");
                            }
                        }
                        finish();
                    }
                }).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                }).setCancelable(true).setTitle(R.string.cast_delete_title)
                .setMessage(R.string.cast_delete_message)

                .create();

    default:
        return super.onCreateDialog(id);
    }
}

From source file:com.grokkingandroid.sampleapp.samples.data.contentprovider.lentitems.CPSampleActivity.java

public void deleteItem(final long itemId) {
    final ContentResolver resolver = getContentResolver();
    new Thread(new Runnable() {
        // for as long as this runs the activity will not be GCed
        public void run() {
            Uri delUri = ContentUris.withAppendedId(Items.CONTENT_URI, itemId);
            int resCount = resolver.delete(delUri, null, null);
            if (resCount == 0) {
                // do s.th. useful
            }/*from w w  w  . ja v a  2s .c o  m*/
        }
    }).start();
    getSupportFragmentManager().popBackStack();
}

From source file:com.amaze.filemanager.asynchronous.asynctasks.DeleteTask.java

private void delete(final Context context, final String file) {
    final String where = MediaStore.MediaColumns.DATA + "=?";
    final String[] selectionArgs = new String[] { file };
    final ContentResolver contentResolver = context.getContentResolver();
    final Uri filesUri = MediaStore.Files.getContentUri("external");
    // Delete the entry from the media database. This will actually delete media files.
    contentResolver.delete(filesUri, where, selectionArgs);
}

From source file:com.manning.androidhacks.hack023.dao.TodoDAO.java

public int deleteTodo(ContentResolver contentResolver, Long id) {
    int ret = 0;/*from   www  .jav a  2s  . com*/

    /* Using the local id */
    int status = getTodoStatus(contentResolver, id);

    switch (status) {
    case StatusFlag.ADD:
        ret = contentResolver.delete(TodoContentProvider.CONTENT_URI, TodoContentProvider.COLUMN_ID + "=" + id,
                null);
        break;
    case StatusFlag.MOD:
    case StatusFlag.CLEAN:
        ContentValues cv = new ContentValues();
        cv.put(TodoContentProvider.COLUMN_STATUS_FLAG, StatusFlag.DELETE);
        contentResolver.update(TodoContentProvider.CONTENT_URI, cv, TodoContentProvider.COLUMN_ID + "=" + id,
                null);
        break;
    default:
        throw new RuntimeException("Tried to delete a todo with invalid status");
    }

    return ret;
}

From source file:th.in.ffc.person.visit.VisitEpiActivity.java

public void doSave() {
    boolean finishable = true;

    for (String key : getDeleteList()) {
        Uri uri = VisitEpi.getContentUri(getVisitNo(), key);

        ContentResolver cr = getContentResolver();
        cr.delete(uri, null, null);
    }/*from   w  ww  . ja v  a  2  s .  c  o  m*/

    ArrayList<String> codeList = new ArrayList<String>();
    for (EditFragment f : getEditFragmentList()) {
        EditTransaction et = beginTransaction();

        if (!f.onSave(et)) {
            finishable = false;
            break;
        }

        String key = et.getContentValues().getAsString(VisitEpi.VACCINE_CODE);
        if (StringArrayUtils.indexOf(codeList, key) >= 0) {
            finishable = false;
            Toast.makeText(this, R.string.duplicate_drug, Toast.LENGTH_SHORT).show();
            break;
        } else
            codeList.add(key);

        et.getContentValues().put(VisitEpi._PCUCODE, getPcuCode());
        et.getContentValues().put(VisitEpi._VISITNO, getVisitNo());
        et.getContentValues().put(VisitEpi._PCUCODEPERSON, getPcucodePerson());
        et.getContentValues().put(VisitEpi._PID, getPid());
        et.getContentValues().put(VisitEpi.DATE_EPI, DateTime.getCurrentDate());
        et.getContentValues().put(VisitEpi._DATEUPDATE, DateTime.getCurrentDateTime());

        if (f.action.equals(Action.INSERT)) {

            Uri uri = et.commit(VisitEpi.CONTENT_URI);

            f.action = Action.EDIT;
            f.key = key;

            Log.d(TAG, "insert " + uri.toString());

        } else if (f.action.equals(Action.EDIT)) {

            Uri uri = VisitEpi.getContentUri(getVisitNo(), f.key);
            int update = et.commit(uri, null, null);

            Log.d(TAG, "update epi=" + update);
        }
    }

    if (finishable) {
        this.finish();
        setResult(RESULT_OK);
    }
}

From source file:com.android.unit_tests.CheckinProviderTest.java

@MediumTest
public void testStatsUpdate() {
    ContentResolver r = getContext().getContentResolver();

    // First, delete any existing data associated with the TEST tag.
    Uri uri = Checkin.updateStats(r, Checkin.Stats.Tag.TEST, 0, 0);
    assertNotNull(uri);/*from  w w w .  j av a 2 s  .c  o  m*/
    assertEquals(1, r.delete(uri, null, null));
    assertFalse(r.query(uri, null, null, null, null).moveToNext());

    // Now, add a known quantity to the TEST tag.
    Uri u2 = Checkin.updateStats(r, Checkin.Stats.Tag.TEST, 1, 0.5);
    assertFalse(uri.equals(u2));

    Cursor c = r.query(u2, null, null, null, null);
    assertTrue(c.moveToNext());
    assertEquals(1, c.getInt(c.getColumnIndex(Checkin.Stats.COUNT)));
    assertEquals(0.5, c.getDouble(c.getColumnIndex(Checkin.Stats.SUM)));
    assertFalse(c.moveToNext()); // Only one.

    // Add another known quantity to TEST (should sum with the first).
    Uri u3 = Checkin.updateStats(r, Checkin.Stats.Tag.TEST, 2, 1.0);
    assertEquals(u2, u3);
    c.requery();
    assertTrue(c.moveToNext());
    assertEquals(3, c.getInt(c.getColumnIndex(Checkin.Stats.COUNT)));
    assertEquals(1.5, c.getDouble(c.getColumnIndex(Checkin.Stats.SUM)));
    assertFalse(c.moveToNext()); // Only one.

    // Now subtract the values; the whole row should disappear.
    Uri u4 = Checkin.updateStats(r, Checkin.Stats.Tag.TEST, -3, -1.5);
    assertNull(u4);
    c.requery();
    assertFalse(c.moveToNext()); // Row has been deleted.
    c.close();
}

From source file:com.chatwing.whitelabel.managers.ConversationModeManager.java

private void markNotificationRead(String conversationID) {
    Uri uri = ChatWingContentProvider.getNotificationMessagesUri();

    ContentResolver contentResolver = mActivityDelegate.getActivity().getContentResolver();
    contentResolver.delete(uri, NotificationMessagesTable.CONVERSATION_ID + "==\"" + conversationID + "\"",
            null);/*ww w.j  a  v  a  2 s .  c  om*/
}

From source file:org.xwalk.runtime.extension.api.messaging.MessagingManager.java

private void operation(int instanceID, JSONObject jsonMsg) {
    JSONObject eventBody = null;/* w  w  w . java  2s  .  c  o m*/
    String promise_id = null, msgType = null, id = null, cmd = null;
    boolean isRead = false;

    try {
        promise_id = jsonMsg.getString("_promise_id");
        eventBody = jsonMsg.getJSONObject("data");
        if (eventBody.has("messageID")) {
            id = eventBody.getString("messageID");
        } else {
            id = eventBody.getString("conversationID");
        }
        cmd = jsonMsg.getString("cmd");
        if (eventBody.has("value")) {
            isRead = eventBody.getBoolean("value");
        }
        msgType = eventBody.getString("type");
    } catch (JSONException e) {
        e.printStackTrace();
        return;
    }

    String selString = null;
    if (eventBody.has("messageID")) {
        selString = String.format("%s = ?", MessagingSmsConsts.ID);
    } else {
        selString = String.format("%s = ?", MessagingSmsConsts.THREAD_ID);
    }

    String[] selArgs = new String[] { id };
    ContentResolver cr = mMainActivity.getContentResolver();
    Uri contentUri = getUri(msgType);

    if (cmd.equals("msg_deleteMessage") || cmd.equals("msg_deleteConversation")) {
        cr.delete(contentUri, selString, selArgs);
    } else if (cmd.equals("msg_markMessageRead") || cmd.equals("msg_markConversationRead")) {
        ContentValues values = new ContentValues();
        values.put("read", isRead ? "1" : "0");
        cr.update(contentUri, values, selString, selArgs);
    }

    JSONObject jsonMsgRet = null;
    try {
        jsonMsgRet = new JSONObject();
        jsonMsgRet.put("_promise_id", promise_id);
        JSONObject jsData = new JSONObject();
        jsonMsgRet.put("data", jsData);
        jsData.put("error", false);
        JSONObject jsBody = new JSONObject();
        jsData.put("body", jsBody);
        if (eventBody.has("messageID")) {
            jsBody.put("messageID", id);
        } else {
            jsBody.put("conversationID", id);
        }
        jsonMsgRet.put("cmd", cmd + "_ret");
    } catch (JSONException e) {
        e.printStackTrace();
        return;
    }

    mMessagingHandler.postMessage(instanceID, jsonMsgRet.toString());
}

From source file:th.in.ffc.person.visit.VisitDrugActivity.java

private void doSave() {
    boolean finishable = true;
    for (String code : getDeleteList()) {

        Uri uri = VisitDrug.getContentUriId(Long.parseLong(getVisitNo()), code);

        ContentResolver cr = getContentResolver();
        int count = cr.delete(uri, null, null);
        Log.d(TAG, "deleted drug count=" + count + " uri=" + uri.toString());
    }/* w w w. j  a v  a 2s . c  o  m*/

    ArrayList<String> codeList = new ArrayList<String>();
    for (String tag : getEditList()) {
        DrugFragment f = (DrugFragment) getSupportFragmentManager().findFragmentByTag(tag);
        if (f == null)
            continue;

        EditTransaction et = beginTransaction();
        f.onSave(et);

        if (!et.canCommit()) {
            finishable = false;
            break;
        }

        String code = et.getContentValues().getAsString(VisitDrug.CODE);
        if (isAddedCode(codeList, code)) {
            finishable = false;
            Toast.makeText(this, R.string.duplicate_drug, Toast.LENGTH_SHORT).show();
            break;
        } else
            codeList.add(code);

        et.getContentValues().put(VisitDrug.NO, getVisitNo());
        et.getContentValues().put(VisitDrug._PCUCODE, getPcuCode());
        et.getContentValues().put(VisitDrug._DATEUPDATE, DateTime.getCurrentDateTime());
        et.getContentValues().put(VisitDrug.DOCTOR1, getUser());

        if (f.action.equals(Action.INSERT)) {

            Uri uri = et.commit(VisitDrug.CONTENT_URI);

            f.action = Action.EDIT;
            f.key = code;

            Log.d(TAG, "insert drug=" + uri.toString());

            Answers.getInstance()
                    .logCustom(new CustomEvent("Drug")
                            .putCustomAttribute("cost", et.getContentValues().getAsFloat(VisitDrug.COST))
                            .putCustomAttribute("user", getUser()).putCustomAttribute("pcu", getPcuCode())
                            .putCustomAttribute("code", code));
        } else if (f.action.equals(Action.EDIT)) {

            Uri updateUri = VisitDrug.getContentUriId(Long.parseLong(getVisitNo()), f.key);
            int update = et.commit(updateUri, null, null);
            Log.d(TAG, "update drug=" + update + " uri=" + updateUri.toString());
        }

    }

    if (finishable)
        this.finish();

}

From source file:name.zurell.kirk.apps.android.rhetolog.RhetologApplication.java

@Override
public void onSessionDelete(Context context, Uri session) {
    ContentResolver cr = getContentResolver();

    // Remove session from session list, and remove all related participant and event records.
    cr.delete(session, null, null);
}