Android Dialogs: Confirmation for deleting a table row
<p>I have a table that I am trying to dynamically delete rows from.  Each row has a delete button, which opens a confirmation dialog.  I would like to remove this table row when confirmation gives a positive result.  </p>

<p>I got this working, rather sloppily, and was wondering if there was a simpler way to accomplish my goal.  To clarify things in the code sample, my table needs to be created dynamically, so I gave the delete buttons an id of the row id + 2000.  I also destroy and recreate the dialog in each onPrepareDialog().  Is there a cleaner way to do this, specifically without destroying and recreating the dialog each time it is opened?  Thank you very much!</p>

<p>Some code from my main Activity class:</p>

<pre><code>    private OnClickListener deleteRowListener = new OnClickListener() {

                public void onClick(View v) {
                    Bundle args = new Bundle();
                            args.putInt(DELETE_ID_KEY, v.getId());
                    showDialog(DIALOG_DELETE,args);
                }
        };



        @Override
        protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {

            switch (id) {
                case DIALOG_DELETE : {
                    removeDialog(id);
                    dialog = createDeleteDialog(args);
                }
            }
        }

        @Override
        protected Dialog onCreateDialog(int id, Bundle args) {
            switch (id) {
                case DIALOG_DELETE : {
                    return createDeleteDialog(args);
                }
            }
            return null;
        }

private Dialog createDeleteDialog(Bundle args) {
        final int toDeleteId = args.getInt(DELETE_ID_KEY) - 2000;  //FSP!!
        return new AlertDialog.Builder(this)
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
             TableRow row = rowsMap.get(toDeleteId);  
                 myTable.removeView(row);
            }
        })
        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                dialog.cancel();
            }
        })
        .create();
    }
</code></pre>
