AlertDialog with two editors - when you click ok & # 8594; start a new alert

its a very silly question, but I started the program with java for android just a few days ago and I don’t know what to do ....

So, I want AlertDialog when you click the button. AlertDialog requires 2 Edits for this. when u click OK, he needs to calculate something and show the solution in the new AlertDialog.

here is what i got:

  public void btn_own(View view) {

        int a, b, c;      
        final String s;

        AlertDialog.Builder alert = new AlertDialog.Builder(this);

            alert.setTitle("Enter range");

            final EditText inputa = new EditText(this);
            final EditText inputb = new EditText(this);
            alert.setView(inputa);
            alert.setView(inputb);

                alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        int aa, bb, cc , e ;
                        aa = new Integer(String.valueOf(inputa));
                        bb = new Integer(String.valueOf(inputb));
                        cc = bb - aa;
                        Random rand = new Random();
                        int d = rand.nextInt(cc);
                        e = d + bb;
                        Integer out = new Integer(e);


                        s = out.toString(); 

                        new AlertDialog.Builder(this)  
                                .setMessage(s)
                                .show();
                    }
                });

    }

The last "this" is invalid. I get a message:

Builder(android.content.Context) in Builder cannot be applied to (android.content.DialogInterface.OnClickListener)

but I don’t know what I can write instead of 'this'

Another problem is that 's' is marked in the line above 'this'.  Cannot assign a value to final variable 's'

I hope you can help me


EDIT:

My new code is:

public void btn_own(View view) {
    int a, b, c;        // a : untere Grenze , b : obere Grenze


    AlertDialog.Builder alert = new AlertDialog.Builder(this);

        alert.setTitle("Enter range");

        final EditText inputa = new EditText(this);
        final EditText inputb = new EditText(this);
        alert.setView(inputa);
        alert.setView(inputb);

            alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    int aa = 0;
                    int bb = 0;
                    int cc = 0;
                    int e = 0;
                    try {
                        aa = Integer.parseInt(inputa.getText().toString());
                    }   catch (NumberFormatException f) {
                        System.out.println("not a number: " + inputa.getText().toString());
                        f.printStackTrace();
                    }

                    try {
                        bb = Integer.parseInt(inputb.getText().toString());
                    }   catch (NumberFormatException g) {
                        System.out.println("not a number: " + inputb.getText().toString());
                        g.printStackTrace();
                    }
                    cc = bb - aa;
                    Random rand = new Random();
                    int d = rand.nextInt(cc);
                    e = d + bb;
                    Integer out = new Integer(e);
                    s = out.toString();

                    new AlertDialog.Builder(Decider.this)
                            .setMessage(s)
                            .show();

                }
            });
        alert.show();



}    

: , : , . : , ? ?

+4
4

DialogInterface.OnClickListener ( , , )

public class MyActivity extends Activity implements {
    public void onClick(DialogInterface dialog, int whichButton) {
         int aa, bb, cc, e;
         aa = new Integer(String.valueOf(inputa));
         bb = new Integer(String.valueOf(inputb));
         cc = bb - aa;
         Random rand = new Random();
         int d = rand.nextInt(cc);
         e = d + bb;
         Integer out = new Integer(e);
         s = out.toString(); 
         new AlertDialog.Builder(this).setMessage(s).show();
    }
}

:

alert.setPositiveButton("Ok", this);
+3

AlertDialogs AlertDialogs.

, :

 new AlertDialog.Builder(this)  
                        .setMessage(s)
                        .show();

,

YourActivity.this view.getContext()


: NumberFormatException, :

 try {
      aa = Integer.parseInt(inputa.getText().toString());
 } catch (NumberFormatException e) {
      System.out.println("not a number: " + inputa.getText().toString());
      e.printStackTrace();
 }

, editText !

+2

view.getContext() this, OnClickListener AlertDialog :

AlertDialog.Builder alert = new AlertDialog.Builder(view.getContext());

-, s "", "" final s btn_own.

+2

int :

aa = Integer.parseInt(inputa.getText()));

try/catch,

0

All Articles