user1618287
user1618287

Reputation: 23

Multiple dialog popping up on a single call android

I am pretty new to this so please forgive for my naivety. I have properly searched this problem in the forum and couldnot find the resolution, but then it could be that no one has been careless enough to make a mistake like me .. :)

I have two buttons on a form from where I am calling in two dialog boxes. I have an Onprepare method and On create method for the dialog which I am handling via a switch and case.

        public void onClick(View arg0) {
    // TODO what needs to be done on button clicks
    switch (arg0.getId()) {
    case R.id.bAddtrans:

    case R.id.btransdate:
        showDialog(1);
        date.setText(strDate);
    case R.id.bpaidfor:
        showDialog(2);
    }

My On Prepare and On Create methods are :

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
    super.onPrepareDialog(id, dialog);
    switch (id) {
    case 1:
        // Some initialization needed.
        DatePickerDialog dateDlg = (DatePickerDialog) dialog;
        int iDay,
        iMonth,
        iYear;
        Calendar cal = Calendar.getInstance();
        iDay = cal.get(Calendar.DAY_OF_MONTH);
        iMonth = cal.get(Calendar.MONTH);
        iYear = cal.get(Calendar.YEAR);
        dateDlg.updateDate(iYear, iMonth, iDay);
        break;
    case 2:
        // Static dialog hence no initialization needed
        break;
    }
    return;

}

protected Dialog onCreateDialog(int id) {
    switch (id) {
    case 1:
        DatePickerDialog dateDlg = new DatePickerDialog(this,
                new DatePickerDialog.OnDateSetListener() {
                    public void onDateSet(DatePicker view, int year,
                            int monthOfYear, int dayOfMonth) {
                        Time chosenDate = new Time();
                        chosenDate.set(dayOfMonth, monthOfYear, year);
                        long dtDob = chosenDate.toMillis(true);
                        strDate = DateFormat.format("MMMM dd, yyyy", dtDob);
                        // Toast.makeText(this, "Date picked: " + strDate,
                        // Toast.LENGTH_SHORT).show(); }
                    }
                }, 2011, 0, 1);
        dateDlg.setMessage("Please select date..");
        /*Toast toast = Toast.makeText(this, "Date picked: " + strDate,
                Toast.LENGTH_SHORT);
        toast.show();*/
        return dateDlg;
        //break;
    case 2:
        // TODO show multiselect dialog box
        // ArrayList<String> adapterpaidfor = new ArrayList<String>(Data);
        final CharSequence[] peoplelist = Data
                .toArray(new CharSequence[Data.size()]);
        final ArrayList<CharSequence> selectedpeople = new ArrayList<CharSequence>();
        boolean[] checkedpeople = new boolean[peoplelist.length];
        int count = peoplelist.length;

        for (int i = 0; i < count; i++)
            checkedpeople[i] = selectedpeople.contains(peoplelist[i]);

        DialogInterface.OnMultiChoiceClickListener peopleDialogListener = new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which,
                    boolean isChecked) {
                if (isChecked)
                    selectedpeople.add(peoplelist[which]);
                else
                    selectedpeople.remove(peoplelist[which]);

                onChangeSelectedpeople();
            }

            private void onChangeSelectedpeople() {
                // TODO change the button name
                StringBuilder stringBuilder = new StringBuilder();

                for (CharSequence peoplelist : selectedpeople)
                    stringBuilder.append(peoplelist + ",");

                paidfor.setText(stringBuilder.toString());
            }
        };

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Select People");
        builder.setMultiChoiceItems(peoplelist, checkedpeople,
                peopleDialogListener);
        builder.setPositiveButton( "OK", onChangeSelectedpeople());

        AlertDialog dialog = builder.create();
        dialog.show();
        return dialog;
        //break;
    }
    return null;    

}

private android.content.DialogInterface.OnClickListener onChangeSelectedpeople() {
    // TODO Auto-generated method stub
    return null;
}

The Problem is that when i click on the button btransdate the dialog for case 2 also pops up on the frontend I have to press Ok button of the dialog and then I can find my 2nd dialog box on the background having the datepicker. When i click the button bpaidfor every thing work fine and the datepicker dialog box is not called.

Upvotes: 0

Views: 1174

Answers (2)

Andras K
Andras K

Reputation: 932

In your onClick method you should write a break; command after each case, like this:

public void onClick(View arg0) {
// TODO what needs to be done on button clicks
switch (arg0.getId()) {
case R.id.bAddtrans:
break;
case R.id.btransdate:
    showDialog(1);
    date.setText(strDate);
break;
case R.id.bpaidfor:
    showDialog(2);
break;
}

Upvotes: 0

ahodder
ahodder

Reputation: 11439

In your first block of code, you are missing your break statements. You are falling through the switch statement and subsequently making all of the called before returning.

Upvotes: 2

Related Questions