user1341676
user1341676

Reputation:

Android: AlertDialog inside another AlertDialog's listener

UPDATE: Something must've been wrong with Eclipse, since I didn't get any logged errors on the crash. I not get this error when the app crashes as described:

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

This occurs when running the noteview.show() method the second time.


Original post

I have a problem with an AlertDialog, which is started from inside the listener of another AlertDialog.

Here's the code for creating the dialogs and the listener and show() of the dialog(s):

AlertDialog.Builder mdialog;
ArrayAdapter<String> popmenu;
EditText input;

mdialog = new AlertDialog.Builder(ShoppingListApp03Activity.this);
popmenu  = new ArrayAdapter<String>(ShoppingListApp03Activity.this,
           android.R.layout.select_dialog_item);
popmenu.add("Notes");
popmenu.add("Remove");
noteview = new AlertDialog.Builder(ShoppingListApp03Activity.this)
noteview.setTitle("Notes");
input = new EditText(this);
noteview.setView(input);

lv.setOnItemLongClickListener(new OnItemLongClickListener() {
   @Override
       public boolean onItemLongClick(AdapterView<?> parent, View view, int position,
                                                     long id) {
           final String sitem = items.get(position).getId();
           mdialog.setAdapter(popmenu, new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int item) {
                 if (item == 0) {
                    updateFooter("Not implemented. (" + sitem + ")");
                noteview.show();
                 } else {
                    deleteitem = Integer.parseInt(sitem);
                    rmvdialog.show();
                 }
              }
           });

           mdialog.show();
           return false;
        }
     });

/* Click listener for the "Notes" popup menu: */
noteview.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        Editable value = input.getText();
        // Do something with value!
    }
});
noteview.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        // Canceled.
    }
});

The onItemLongClickListener opens a popup menu which presents two choices.

The second, which starts rmvdialog(), does its job well. No problems there.

The first, which opens the notes dialog, has a problem: On the first access, it opens the notes dialog and presents the message and the OK and Cancel buttons. However, after returning to the list (main view), either by clicking OK or Cancel in the notes dialog (since neither does anything yet), a second long click and selection of the notes dialog crashes the application.

The crash is without error logging. A try-catch around noteview.show() doesn't reveal anything either.

I am going blind looking at my own code, so there's probably something rotten in the design, but I can't see what.

The overall functionality here is a list with several items. When long-clicking on one of the items, a popup menu with two entries shows. Then, when long-clicking on the one called "notes", a dialog for showing/editing text, for the item in the list, is presented.

Upvotes: 0

Views: 2471

Answers (1)

WarrenFaith
WarrenFaith

Reputation: 57672

As you too just found out the same I did. Here is my test activity which I requested from you. As you can see it is a stripped down version of yours (except I use onclick instead of onlongclick)

public class MainActivity extends Activity {

    private Builder firstDialogBuilder;
    private AlertDialog firstDialog;
    private Builder secondDialogBuilder;
    private AlertDialog secondDialog;
    private ListView secondListView;
    private ListView firstListView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        firstDialogBuilder = new AlertDialog.Builder(this);
        firstListView = new ListView(this);
        firstListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new String[] {"First Item1", "First Item2"}));
        firstDialogBuilder.setView(firstListView);
        firstListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                secondListView = new ListView(MainActivity.this);
                secondListView.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, new String[] {"Second Item1", "Second Item2"}));
                secondDialogBuilder.setView(secondListView);
                secondDialog = secondDialogBuilder.create();
                secondDialog.show();
            }
        });

        secondDialogBuilder = new AlertDialog.Builder(this);
        secondListView = new ListView(this);
        secondListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new String[] {"Second Item1", "Second Item2"}));
        secondDialogBuilder.setView(secondListView);

        secondDialogBuilder.setPositiveButton("ok", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(), "ok on second", Toast.LENGTH_SHORT).show();
                secondDialog.dismiss();
            }
        });
        secondDialogBuilder.setNegativeButton("cancel", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(), "cancel on second", Toast.LENGTH_SHORT).show();
                secondDialog.dismiss();
            }
        });

        firstDialog = firstDialogBuilder.create();
        firstDialog.show();
    }
}

For the next time, please provide something like that to help us helping you :)

Upvotes: 1

Related Questions