G
G
gibsonen2017-09-25 10:59:42
Java
gibsonen, 2017-09-25 10:59:42

AlertDialog works only the second time, why?

Hello, the problem is this:
I have a button in a menu item, after pressing which an AlertDialog should open. But on the first click on the button, nothing happens (AlertDialog does not fire), AlertDialog only fires on the second click on the button.
What could it be? I am attaching below the code for handling this button from the menu item:

public void settingCount(MenuItem item) {
        item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {
                LayoutInflater layoutInflater = LayoutInflater.from(context);
                View view = layoutInflater.inflate(R.layout.setting_dialog, null);
                AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
                dialogBuilder.setView(view);
                final EditText editTextString = (EditText) view.findViewById(R.id.editTextDialogString};    
                dialogBuilder
                        .setCancelable(false)
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            ///.....///
                        })
                        .setNegativeButton("Отмена",
                                new DialogInterface.OnClickListener(){
                                   ///.....////
                                });
                AlertDialog alertDialog = dialogBuilder.create();
                alertDialog.show();
                return true;
            }
        });
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Myvrenik, 2017-09-25
@gibsonen

In a comment, you showed how the settingCount method is called :

<item
    ...
    android:onClick="settingCount" />

This is where the problem lies. The settingCount method is called on the first click, another handler is added in this method, and only then all subsequent clicks on the button are already processed by it.
Replace the method code with this:
public void settingCount(MenuItem item) {
                LayoutInflater layoutInflater = LayoutInflater.from(context);
                View view = layoutInflater.inflate(R.layout.setting_dialog, null);
                AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
                dialogBuilder.setView(view);
                final EditText editTextString = (EditText) view.findViewById(R.id.editTextDialogString};    
                dialogBuilder
                        .setCancelable(false)
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            ///.....///
                        })
                        .setNegativeButton("Отмена",
                                new DialogInterface.OnClickListener(){
                                   ///.....////
                                });
                AlertDialog alertDialog = dialogBuilder.create();
                alertDialog.show();
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question