Question

I have an application that when a button is pressed, a dialog box containing a list will appear and after selecting a certain item in the list, a number picker dialog then appears. What I want to happen is that after the user selects a number from the number picker dialog, only the number picker dialog will close, leaving the dialog box still up.

This is my code so far.

This is the code for the first dialog box (the one with the list):

private void generateUnitDialog(String[] productUnit) {

    final CharSequence[] items = productUnit;

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Make your selection");

    builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // Handle Confirm action here
        }
    });

    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // Handle cancel action here
        }
    });
    builder.setItems(items, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            // Do something with the selection
            Toast.makeText(getApplicationContext(),
                    "Item Click detected", Toast.LENGTH_SHORT)
                    .show();

            generateQuantityDialog();

        }
    });

    builder.setCancelable(false);

    AlertDialog alert = builder.create();
    alert.show();

}

When a certain list item is selected, the numberpicker dialog then pops up using this code:

public int generateQuantityDialog(){

         final Dialog d = new Dialog(com.angelo.orderform.OrderForm.this);

         d.setTitle("Choose Quantity");
         d.setContentView(R.layout.dialog);

         Button b1 = (Button) d.findViewById(R.id.button1);
         Button b2 = (Button) d.findViewById(R.id.button2);

         final NumberPicker np = (NumberPicker) d.findViewById(R.id.numberPicker1);

         np.setMaxValue(100000);
         np.setMinValue(1);
         np.setWrapSelectorWheel(true);
         //np.setOnValueChangedListener(this);

         b1.setOnClickListener(new OnClickListener(){
             @Override
             public void onClick(View v) {
                 //tv.setText(String.valueOf(np.getValue()));
                 d.dismiss();
             }    
        });
        b2.setOnClickListener(new OnClickListener(){
          @Override
          public void onClick(View v) {
              d.dismiss();
           }    
          });
       d.show();

       return(np.getValue());


}

After the user selects a certain number, the number picker dialog closes as well as the one with the list. I want the dialog with the list to stay on.

Apparently, setCancellable(false) does not help.

Any ideas? Even if it's a different approach, I'm open ears.

Était-ce utile?

La solution 2

Okay I figured it out but I think it's bad practice.

What I did was I placed the code that generates the number picker dialog and placed it to where generateQuantityDialog() originally was and just called builder.show() after d.dismiss().

I'll look for a work around, ditching this approach.

Autres conseils

You have to set AlertDialog alert variable global.

as per your require you can cancel or dismiss alertdialog.

b1.setOnClickListener(new OnClickListener(){
             @Override
             public void onClick(View v) {
                 //tv.setText(String.valueOf(np.getValue()));
                 d.dismiss();
                 alert.dismiss() ; or 
                 alert.cancel();
             }    
        });

You can dismiss alertDialog as per your requirement.

like if select second dialog.

public class MainActivity extends Activity {
 Dialog d;
  AlertDialog.Builder builder;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


      final CharSequence[] items = {"a","b","c"};

        builder = new AlertDialog.Builder(this);
        builder.setTitle("Make your selection");

        builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // Handle Confirm action here
            }
        });

        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // Handle cancel action here
            }
        });
        builder.setItems(items, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                // Do something with the selection
                Toast.makeText(getApplicationContext(),
                        "Item Click detected", Toast.LENGTH_SHORT)
                        .show();

                generateQuantityDialog();

            }
        });

        builder.setCancelable(false);

        AlertDialog alert = builder.create();
        alert.show();

    }

public int generateQuantityDialog(){

     d = new Dialog(this);

    d.setTitle("Choose Quantity");
    d.setContentView(R.layout.al);

    Button b1 = (Button) d.findViewById(R.id.button1);
    Button b2 = (Button) d.findViewById(R.id.button2);

   //        final NumberPicker np = (NumberPicker) d.findViewById(R.id.timePicker1);

   //        np.setMaxValue(100000); 
   //        np.setMinValue(1);
        //        np.setWrapSelectorWheel(true);
    //np.setOnValueChangedListener(this);

    b1.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            //tv.setText(String.valueOf(np.getValue()));
            d.dismiss();
            AlertDialog alert = builder.create();
                alert.show();

        }


   });
   b2.setOnClickListener(new OnClickListener(){
     @Override
     public void onClick(View v) {
         d.dismiss();
            AlertDialog alert = builder.create();
                alert.show();

     }


     });
  d.show();

  return(54);


  }
  }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top