私はそれぞれのデータに従って更新したいEditTextフィールドを複数持っていますので、私はaddTextChangedListener
を使っています。私の目標は、2つのEditText値の合計を取得し、その答えを別のEditTextに表示することです。しかし、私はまた、3番目のEditTextと最初の2つのうちの1つの違いを最初のものに表示しようとしています。だから、amountreceived = totalprice + tip
、私はそれに応じて更新したいだけでなく、ユーザーが受信した合計価格と金額にデータを入力した場合、私はtip = amountreceived - totalprice
になりたいと思います。次のように合計を得るためにeditText値を更新する
私のコードは次のとおりです。
@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();
}
私はこれが働くだろうと思ったが、私は、データを入力するためのEditTextフィールドをクリックすると、アプリがクラッシュします。私もlogcatにエラーがないので、何が間違っているのか分かりません。どんな助けでも大歓迎です!
EDIT:次のようにlogcatエラーがある:
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
スタックトレースはありませんか?これは共通の例外を与えるはずの非常にシンプルなアプリのようです。おそらく、あなたのXMLレイアウトで見つけようとしているidのどれかを見つけることができないかもしれません。 – Neil
実際には、Android Studioを終了してバックアップを読み込んだ後、エラーが表示されます。私は私の質問を更新します。 –