Reputation: 991
I can't add double digits in Editext and calculate the final price. I don't have any problems with one digit numbers. but when I want to add two-digit numbers My app closed.
edt_value.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if (TextUtils.isEmpty(edt_value.getText())){
edt_value.clearFocus();
txt_finalprice.setText(gl_price.toString());
}else {
if(getCurrentFocus() == edt_value) {
int value = Integer.parseInt(edt_value.getText().toString().trim());
int price = Integer.parseInt(txt_finalprice.getText().toString().trim());
txt_finalprice.setText(value * price + "$");
}
}
}
});
Upvotes: 0
Views: 40
Reputation: 8305
You may getting the calculated data which is out of integer range use Double.parseDouble
instead of Integer.parseInt
Update
Here is the problem in this code:
int value = Integer.parseInt(edt_value.getText().toString().trim());
int price = Integer.parseInt(txt_finalprice.getText().toString().trim());
txt_finalprice.setText(value * price + "$");
you are getting price from txt_finalprice that is fine at first and then in the last statement your are setting value * price + "$" to txt_finalprice, now txt_finalprice has some number and $(for ex: 30$), when you type again something in edit text this time again you are converting txt_finalprice
value to price, but this will raise an exception because txt_finalprice contains $ and it cannot be converted to int.
You need to store txt_finalprice
value to a variable.
Upvotes: 1
Reputation: 1151
int finalValue = value * price;
txt_finalprice.setText(string.valueof(finalValue)+ "$");
Upvotes: 0