J'ai plusieurs champs EditText dans lesquels je voudrais mettre à jour en fonction des données de chacun, donc j'utilise addTextChangedListener
pour eux. Mon but est d'obtenir la somme de deux valeurs EditText et d'afficher la réponse dans un autre EditText. Cependant, j'essaie également d'obtenir la différence du troisième EditText et l'un des deux premiers à afficher dans le premier. Donc amountreceived = totalprice + tip
, je veux mettre à jour en conséquence, ainsi que si l'utilisateur a entré des données dans le prix total et le montant reçu, je voudrais qu'il soit tip = amountreceived - totalprice
.Mise à jour des valeurs editText pour obtenir la somme ou la différence
Mon code est le suivant:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.order_details);
eAmountReceived = (EditText) findViewById(R.id.amountReceived);
eTotalCost = (EditText) findViewById(R.id.orderTotal);
eTip = (EditText) findViewById(R.id.tip);
eMileage = (EditText) findViewById(R.id.mileage);
eGrandTotal = (EditText) findViewById(R.id.grandTotal);
eAmountReceived.addTextChangedListener(this);
eTip.addTextChangedListener(this);
// eTotalCost.addTextChangedListener(this);
// eMileage.addTextChangedListener(this);
// eGrandTotal.addTextChangedListener(this);
eTip.addTextChangedListener(new TextWatcher()
{
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after){}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count)
{
if(eTip.isFocused() && isSet(eAmountReceived) && isSet(eTotalCost))
{
updateSubtract();
}
}
@Override
public void afterTextChanged(Editable s){}
});
eAmountReceived.addTextChangedListener(new TextWatcher()
{
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after){}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count)
{
if(eAmountReceived.isFocused() && isSet(eTotalCost) && isSet(eTip))
{
updateAdd();
}
}
@Override
public void afterTextChanged(Editable s){}
});
}
private void updateAdd()
{
double dTotalCost = Double.valueOf(eTotalCost.getText().toString());
double dTip = Double.valueOf(eTip.getText().toString());
eAmountReceived.setText(String.valueOf(dTotalCost + dTip));
}
private void updateSubtract()
{
double dAmountReceived = Double.valueOf(eAmountReceived.getText().toString());
double dTotalCost = Double.valueOf(eTotalCost.getText().toString());
eTip.setText(String.valueOf(dAmountReceived - dTotalCost));
}
private boolean isSet(EditText editText)
{
return !editText.getText().toString().matches("");
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2)
{
updateAdd();
updateSubtract();
}
@Override
public void afterTextChanged(Editable editable) {}
public void onClick(View view)
{
name = (EditText) findViewById(R.id.name);
number = (EditText) findViewById(R.id.number);
address = (EditText) findViewById(R.id.address);
orderTotal = (EditText) findViewById(R.id.orderTotal);
amountReceived = (EditText) findViewById(R.id.amountReceived);
tip = (EditText) findViewById(R.id.tip);
mileage = (EditText) findViewById(R.id.mileage);
grandTotal = (EditText) findViewById(R.id.grandTotal);
String cName = name.getText().toString();
String num = number.getText().toString();
String cAddress = address.getText().toString();
String cOrderTotal = orderTotal.getText().toString();
String cAmountReceived = amountReceived.getText().toString();
String cTip = tip.getText().toString();
String cMileage = mileage.getText().toString();
String cGrandTotal = grandTotal.getText().toString();
int id = db.addContact(new PhoneBook(cName, num, cAddress, cOrderTotal,
cAmountReceived, cTip, cMileage, cGrandTotal));
contactList.add(new PhoneBook(id, cName, num, cAddress, cOrderTotal,
cAmountReceived, cTip, cMileage, cGrandTotal));
adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "Entry Successfully Created.", Toast.LENGTH_LONG).show();
}
Je pensais que cela fonctionnerait, mais lorsque je clique sur un champ EditText pour saisir les données, les accidents de l'application. Je n'ai pas non plus d'erreur dans mon logcat, donc je ne sais pas ce que je fais de mal. Toute aide serait grandement appréciée!
EDIT: L'erreur logcat est la suivante:
FATAL EXCEPTION: main
Process: com.example.boley.databaseexample, PID: 2949
java.lang.NumberFormatException: Invalid double: ""
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseDouble(StringToReal.java:267)
at java.lang.Double.parseDouble(Double.java:301)
at java.lang.Double.valueOf(Double.java:338)
at com.example.boley.databaseexample.OrderDetails.updateAdd(OrderDetails.java:193)
at com.example.boley.databaseexample.OrderDetails.onTextChanged(OrderDetails.java:215)
at android.widget.TextView.sendOnTextChanged(TextView.java:7991)
at android.widget.TextView.handleTextChanged(TextView.java:8053)
at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:10157)
at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:1033)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:559)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:492)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:491)
at android.text.method.NumberKeyListener.onKeyDown(NumberKeyListener.java:121)
at android.widget.TextView.doKeyDown(TextView.java:6098)
at android.widget.TextView.onKeyDown(TextView.java:5911)
at android.view.KeyEvent.dispatch(KeyEvent.java:2640)
at android.view.View.dispatchKeyEvent(View.java:9234)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at com.android.internal.policy.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:2395)
at com.android.internal.policy.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1727)
at android.app.Activity.dispatchKeyEvent(Activity.java:2725)
at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:543)
at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:53)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:315)
at com.android.internal.policy.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2310)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4127)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4089)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3787)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3844)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3820)
at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3981)
at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2253)
at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1874)
at android.view.inputmethod.InputMethodMan
Etes-vous sûr qu'il n'y a aucune trace de pile? Cela semble être une application assez simple qui devrait donner une exception commune. Peut-être qu'il ne peut pas trouver l'un des ID que vous essayez de trouver dans votre mise en page xml – Neil
En fait, après avoir quitté Android Studio et le recharger, il montre l'erreur. Je vais mettre à jour ma question. –