I created a Dialog with a TextEdit as a number input to convert it to a double value.
I want to prevent an exception by checking, if the TextEdit input is empty but it always jumps to the else statement and therefore creates an exception when the EditText is blank.
final Builder builder = new Builder(this);
final EditText input = new EditText(this);
input.setKeyListener(DigitsKeyListener.getInstance(false, true));
builder
.setTitle(R.string.dialog_title_addmark)
.setMessage(R.string.dialog_addmark)
.setView(input)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (input.toString().trim().length() == 0) {
Toast.makeText(Marks.this, R.string.input_empty, Toast.LENGTH_SHORT).show();
} else {
Double value = Double.valueOf(input.getText().toString());
db.insertMark(selection, value);
getData();
}
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
I also tried it checking
(input.toString() != null)
but that does not work either.
Thanks for your help
If you want the text from a
EditTextobject, you should use the following line of code:where
inputis aEditText-object.Using the code
will give you a string representation of the object, and as long as the object is not
null, the toString of that object will be notnull, so usinginput.toString() != nullwill not give you any useful information.That means that you should do the following validation:
if (input.getText().toString().trim().length() == 0)to figure out if the input object contains some text.