Harald Wilhelm
Harald Wilhelm

Reputation: 6716

Extending AlertDialog

I need to store an additional value (tag) within AlertDialog so I did extend this class. When using this new class it I do get an ClassCastException. What's wrong with my code?

Here's my extended AlertDialog:

public class MyAlertDialog extends AlertDialog {

    private long tag;

    public MyAlertDialog(final Context context) {
        super(context);
    }

    public MyAlertDialog(final Context context, final int theme) {
        super(context, theme);
    }

    public MyAlertDialog(final Context context, final boolean cancelable, final OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public long getTag() {
        return tag;
    }

    public void setTag(final long tag) {
        this.tag = tag;
    }
}

And this is part of an ListActivity using the new class:

public class MyListActivity extends ListActivity {
    private MyAlertDialog alertDialog;
    private long          id;

    @Override
    public boolean onContextItemSelected(final MenuItem menuItem) {
        // ..
        processAlertDialog(id);
        // ..
        return super.onContextItemSelected(menuItem);
    }

    private void processAlertDialog(final long id) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setCancelable(false);
        alertDialogBuilder.setMessage(R.string.txt_reallydelete);
        alertDialogBuilder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(final DialogInterface dialogInterface, final int which) {
                dialogInterface.dismiss();
            }
        } );
        alertDialogBuilder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(final DialogInterface dialogInterface, final int which) {
                dialogInterface.dismiss();

                // do Something
            }
        } );

        // ClassCastException
        alertDialog = (MyAlertDialog) alertDialogBuilder.create();
        alertDialog.setTag(id);
        alertDialog.show();
    }
}

Many thanks in advance.

EDIT:

Uncaught handler: thread main exiting due to uncaught exception
java.lang.ClassCastException: android.app.AlertDialog
at com.test.app.MyListActivity.processAlertDialog(MyListActivity.java:234)
at com.test.app.MyListActivity.onContextItemSelected(MyListActivity.java:99)
at android.app.Activity.onMenuItemSelected(Activity.java:2174)
at com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback.onMenuItemSelected(PhoneWindow.java:2731)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:139)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:129)
at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:884)
at android.widget.AdapterView.performItemClick(AdapterView.java:284)
at android.widget.ListView.performItemClick(ListView.java:3285)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:1640)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4363)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method)

Upvotes: 3

Views: 8597

Answers (2)

waqaslam
waqaslam

Reputation: 68187

To make things simple, you should create a class which extends AlertDialog.Builder.

public class MyAlertDialog extends AlertDialog.Builder {

    //add constructor stuff

    private long tag;
    public long getTag() {
        return tag;
    }

    public void setTag(final long tag) {
        this.tag = tag;
    }
}

and use it in your Activity by calling show() method.

yourBuilder.show();

you dont need to call create() on builder since you are showing it directly

Upvotes: 1

chitacan
chitacan

Reputation: 341

Because when you create your "MyAlertDialog", you didn't create actual "MyAlertDialog" object. You created "AlertDialog.Builder" object and cast it to "MyAlertDialog".

Why are you doing like that??

I think you can create and show your own dialog like this,

private void processAlertDialog(final long id) {
   MyAlertDialog alertdialog = new MyAlertDialog(this);

   alertdialog.setCancelable(false);
   alertdialog.setMessage(R.string.txt_reallydelete);

   // your button's call back

   alertdialog.setTag(id);
   alertdialog.show();
}

Upvotes: 1

Related Questions