1
votes

My Activity Code

Please help me how to delete item in list view that connect to database in Android Studio. This my code :

mainListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

            public boolean onItemLongClick(AdapterView parent, View view, final int position, final long id) {

                final AlertDialog.Builder b = new AlertDialog.Builder(UserList.this);
                b.setIcon(android.R.drawable.ic_dialog_alert);
                b.setMessage("Ingin menghapus data?");
                b.setPositiveButton("Ya",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {

                                IDTable = IDList.get(position);
                                userList.remove(position);
                                UserList.this.listAdapter.notifyDataSetChanged();

                                InfoPokok info = new InfoPokok();
                                info.setId(IDTable);
                                System.out.println("ID : " + info.getId());
                                infoPokokDao.deleteInfoPokok(info);
                            }
                        });
                b.setNegativeButton("Tidak",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                dialog.cancel();
                            }
                        });

                b.show();
                return true;
            }
        });

DAO :

public void deleteInfoPokok(InfoPokok infoPokok) {
String id = infoPokok.getId() + "";

        long deleteId = database.delete(MySQLiteHelper.TABLE_INFO_POKOK, MySQLiteHelper.COLUMN_ID
                + " =?", new String[]{id});
        Cursor cursor = database.query(MySQLiteHelper.TABLE_INFO_POKOK,
                allColumns, MySQLiteHelper.COLUMN_ID + " = " + deleteId, null,
                null, null, null);
        cursor.moveToFirst();
        cursor.close();
    }
3
check your deleteId value if it is -1 it's mean data not get delete.Anjali Tripathi
are u getting any error ..?Moinkhan

3 Answers

1
votes

IN YOUR DISPLAYACTIVITY.JAVA

 btnDelete.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                DataBaseHandler db = new DataBaseHandler(DisplayImageActivity.this);

                Log.d("Delete Image: ", "Deleting.....");
                db.deleteCloth(new Cloth(imageId));

                Intent i = new Intent(DisplayImageActivity.this, MainActivity.class);
                startActivity(i);
                finish();
            }
        });

AND THEN IN DATABASEHANDLER.JAVA

 public void deleteCloth(Cloth cloth) {

        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_CLOTHES, KEY_ID + " = ?", new String[] { String.valueOf(cloth.getID()) });
        db.close();
    }
0
votes

I think the best way to achieve this is to write a delete method in your Provider :

    @Override
public int delete(Uri uri, String selection, String[] selectionArgs) {

    //the provided is requested to delete data - so we'll delete it in the db:
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    int result = db.delete(getTableName(uri), selection, selectionArgs);

    // notify the change
    getContext().getContentResolver().notifyChange(uri, null);

    //return the number of rows deleted
    //it's what we got from the db.delete
    return result;

}

and then just call it like :

getContentResolver().delete(YOUR_CONTENT_URI, String where, String[] selectionArgs);
0
votes

Do u use CursorAdapter or ArrayAdapter?

CursorAdapter - 1. Use AsyncTask or something that u build that delete the data on back thread and not on the UI (currently onLongClick is being called on the UI and you are accessing DB on the UI, bad practice). 2. When the delete is done you can restart your loader to requery the DB and the cursor wil update. Think about maybe showing a progress bar if u will have a large DB or adding calls to server.

ArrayAdapter - Based on array adpter that is a place holder for the cursor. 1. Delete the data from the array and notifyDataSetCahnged, UI thread. 2. Delete the item from DB on back thread. This way the user wont feel any Glitch. Good luck.