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 ?
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With