QNikE
QNikE

Reputation: 53

AlertDialog with two Edits - when press Button ok --> start new Alert

its a very stupid question, but i started program with java for android just a few days ago and i dont know what to do ....

So i want a AlertDialog when u press a Button. This AlertDialog needs 2 Edits. when u press OK it have to calcutale something and show the solution in a new AlertDialog.

thats 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 wrong. i get the Message :

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

but i dont know what i can write in instead of 'this'

a other problem is, that the 's' in the line over 'this' is marked. Cannot assign a value to final variable 's'  

Hope u can help me


EDIT:

My new Code :

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();



}    

error : Ok now it works like : i can enter one number, then i get an other random number. but how can i let it work like : i enter a range, and i get a random number ? why does it view just one edit ?

Upvotes: 4

Views: 1264

Answers (4)

Athanor
Athanor

Reputation: 955

Try to implements DialogInterface.OnClickListener in your activity (it is better because you dont create new object so less resources are used and it saves battery)

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();
    }
}

Then set your button with this:

alert.setPositiveButton("Ok", this);

Upvotes: 3

donfuxx
donfuxx

Reputation: 11321

You should not be nesting AlertDialogs inside other AlertDialogs.

So this:

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

Will use the wrong context, but you will have to pass an activity as the context

Try using YourActivity.this or view.getContext() as param instead


Update: about your NumberFormatException you should try this:

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

But you need to be sure that your editText only allows numbers!!

Upvotes: 2

Athanor
Athanor

Reputation: 955

To convert an int from String use:

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

Surround it with try/catch to avoid user bad input and your exception

Upvotes: 0

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

Use view.getContext() instead of this which refer to the OnClickListener context to create AlertDialog as:

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

Second if you want to change value on s on Alert button click then remove final from the s and declare it outside btn_own method.

Upvotes: 2

Related Questions