Leong Fan
Leong Fan

Reputation: 43

How to set confirmation delete message (AlertDialog) for my list view for android studio

I'm trying to make a confirmation delete message pop out using "AlertDialog" whenever I want to delete an entry on my "listView", the problem is I don't know in which order do I have to put it to make it work.

This is my liewView along with the deleteEntry function

listView = (ListView) findViewById(R.id.listView);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Cursor cursor = (Cursor) parent.getItemAtPosition(position);
            String posID = cursor.getString(0);
            int getID = Integer.parseInt(posID);

            Database_TT db = new Database_TT(DeleteTT.this);

            db.deleteEntry(getID);

        }
    });

This is the AlertDialog I intended to use, it suppose to pop up when i click on an entry, confirming on deletion.

AlertDialog.Builder alert = new AlertDialog.Builder(this);

    alert.setTitle("Delete entry");
    alert.setMessage("Are you sure you want to delete?");
    alert.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
        Database_TT db = new Database_TT(DeleteTT.this);

        public void onClick(DialogInterface dialog, int which) {
            // continue with delete

        }
    });
    alert.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // close dialog
            dialog.cancel();
        }
    });
    alert.show();

I tried putting them together, but I get "variable not initialized". Is there anyway to do this?

I did search through the internet but I cant find any similar problem like mine. I'm a total beginner on android studio btw.

Upvotes: 4

Views: 10811

Answers (1)

Viral Patel
Viral Patel

Reputation: 33438

Try this:

listView = (ListView) findViewById(R.id.listView);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Cursor cursor = (Cursor) parent.getItemAtPosition(position);
        String posID = cursor.getString(0);
        final int getID = Integer.parseInt(posID);

        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setTitle("Delete entry");
        alert.setMessage("Are you sure you want to delete?");
        alert.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
            Database_TT db = new Database_TT(DeleteTT.this);

            public void onClick(DialogInterface dialog, int which) {
                // continue with delete
                Database_TT db = new Database_TT(DeleteTT.this);
                db.deleteEntry(getID);
            }
        });
        alert.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // close dialog
                dialog.cancel();
            }
        });
        alert.show();
    }
});

Upvotes: 8

Related Questions