]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemHolder.java
avoid crash when removing the currency of a row
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / new_transaction / NewTransactionItemHolder.java
index 247f40eb748006c83477c59433661037e31919e6..f60980fa652e1b770909ac3d36b89c46f5a0cea4 100644 (file)
@@ -124,8 +124,9 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                     try {
                         String input = String.valueOf(b.accountRowAccAmounts.getText());
                         input = input.replace(decimalSeparator, decimalDot);
-                        b.accountRowAccAmounts.setText(
-                                String.format("%4.2f", Float.parseFloat(input)));
+                        final String newText = String.format("%4.2f", Float.parseFloat(input));
+                        if (!newText.equals(input))
+                            b.accountRowAccAmounts.setText(newText);
                     }
                     catch (NumberFormatException ex) {
                         // ignored
@@ -206,8 +207,8 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
         b.currencyButton.setOnClickListener(v -> {
             CurrencySelectorFragment cpf = new CurrencySelectorFragment();
             cpf.showPositionAndPadding();
-            cpf.setOnCurrencySelectedListener(
-                    c -> adapter.setItemCurrency(getAdapterPosition(), c.getName()));
+            cpf.setOnCurrencySelectedListener(c -> adapter.setItemCurrency(getAdapterPosition(),
+                    (c == null) ? null : c.getName()));
             cpf.show(activity.getSupportFragmentManager(), "currency-selector");
         });
 
@@ -230,12 +231,12 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                          if (showCurrency) {
                              b.currency.setVisibility(View.VISIBLE);
                              b.currencyButton.setVisibility(View.VISIBLE);
-                             b.currency.setText(mProfile.getDefaultCommodity());
+                             setCurrencyString(mProfile.getDefaultCommodity());
                          }
                          else {
                              b.currency.setVisibility(View.GONE);
                              b.currencyButton.setVisibility(View.GONE);
-                             b.currency.setText(null);
+                             setCurrencyString(null);
                          }
                      });
 
@@ -436,7 +437,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
     private void setCurrencyString(String currency) {
         @ColorInt int textColor = b.dummyText.getTextColors()
                                              .getDefaultColor();
-        if ((currency == null) || currency.isEmpty()) {
+        if (TextUtils.isEmpty(currency)) {
             b.currency.setText(R.string.currency_symbol);
             int alpha = (textColor >> 24) & 0xff;
             alpha = alpha * 3 / 4;