Search code examples
androidandroid-listviewandroid-alertdialogonitemlongclicklistener

Showing a AlertDialog in a onItemLongClick is giving a error about the parent what I have to do?


I am trying to show a dialog when the user do onItemLongClick of a Listview for confirm if the user wants do that but give me this error in the line of the adb.show():

The specified child already has a parent. You must call removeView() on the child's parent first

with the folowing code:

listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

            AlertDialog.Builder adb = new AlertDialog.Builder(getBaseContext());
            adb.setView(view);
            adb.setTitle("Title of alert dialog");
            adb.setIcon(android.R.drawable.ic_dialog_alert);
            adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                    Toast.makeText(Menu.this, "OK", Toast.LENGTH_LONG).show();
                } });

            adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(Menu.this, "cancel", Toast.LENGTH_LONG).show();
                    //finish();
                } });
            adb.show();

            return true;
        }
    });

I searched for answers but I dint saw about listview or longclick so i need help :(

Added the logcat, the line where it give the error is where appear adb.show();:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:3936)
at android.view.ViewGroup.addView(ViewGroup.java:3786)
at android.view.ViewGroup.addView(ViewGroup.java:3758)
at com.android.internal.app.AlertController.setupView(AlertController.java:492)
at com.android.internal.app.AlertController.installContent(AlertController.java:236)
at android.app.AlertDialog.onCreate(AlertDialog.java:356)
at android.app.Dialog.dispatchOnCreate(Dialog.java:373)
at android.app.Dialog.show(Dialog.java:274)
at android.app.AlertDialog$Builder.show(AlertDialog.java:993)
at pt.isec.jogodememoria.MenuEscolheNivel$2.onItemLongClick(MenuEscolheNivel.java:119)
at android.widget.AbsListView.performLongPress(AbsListView.java:3121)
at android.widget.AbsListView$CheckForLongPress.run(AbsListView.java:3070)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Solution

  • A very easy mistake, this is missing AlertDialog alertDialog = adb.create(); and there is no need for the setView. You can still create a separate method and instantiate it for every long item click.

    listView.setOnItemLongClickListener(new AdapterView
                .OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
    
            createDialog(view);
            return true;
        }
    });
    

    Then create a separate method for your dialog:

    public void createDialog(View view){
    
       AlertDialog.Builder adb = new AlertDialog.Builder(this);
        //adb.setView(Main.this);
        adb.setTitle("Title of alert dialog");
        adb.setIcon(android.R.drawable.ic_dialog_alert);
        adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
    
                Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
            } });
    
        adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(), "cancel", Toast.LENGTH_LONG).show();
                //finish();
            } });
    
        AlertDialog alertDialog = adb.create();
        alertDialog.show();
    
    }
    

    enter image description here