开发者

Android Dialogs: Confirmation for deleting a table row

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.

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!

Some code from my main Activity class:

    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(开发者_StackOverflow中文版) {
            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();
    }


this may be helpful (note- that makes use of an API that isn't available in 1.5 - You could do a fairly simple workaround if you need cupcake compatibility, though)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜