So in one of my app's activity, i have listView which consists of an EditText and user enters an amount and I want to display the total amount in one of the textViews. The way I tried is I attached a TextWatcher to the listViews EditText like this
CustomWatcher oldWatcher = (CustomWatcher) holder.tvDonationAmount.getTag();
if (oldWatcher != null)
holder.tvDonationAmount.removeTextChangedListener(oldWatcher);
CustomWatcher newWatcher = new CustomWatcher();
holder.tvDonationAmount.setTag(newWatcher);
holder.tvDonationAmount.addTextChangedListener(newWatcher);
And My Custom TextWatcher's Code goes like this:
public class CustomWatcher implements TextWatcher {
int sum, amount;
FinalDTAdapter finalDTAdapter;
@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 (editable.toString().length() > 0) {
amount = Integer.parseInt(editable.toString());
}
GenRecNew.addn.add(amount);
System.out.println("amount :" + amount);
for (int i = 0; i < GenRecNew.addn.size(); i++) {
sum += GenRecNew.addn.get(i);
}
String res = String.valueOf(sum);
GenRecNew.mFinalDonationAmount.setText(res);
}}
Here the GenRec.addn is an ArrayList of Integers. So here I'm pushing the input amount in the ArrayList and then calculating the sum of all the elements of the array. The problem is when I input the amount example: 500 in the array it pushes 5 then 50 then 500 and the total amount is calculated wrong. Can you guys help me to push the final amount entered into the array? I know onFocusChangeListner helps but please tell me how, not able to figure it out. Thanks in advance.
The problem here is that the TextWatcher
processes the data instantly. What you need to do is debounce the operation so that it does the calculation only if a certain delay is done. You can use a Timer to achieve this (or if you're using Rx in your application then use the RxTextView's debounce operation). Here's how to do it in vanilla Android code:
public class CustomWatcher implements TextWatcher {
private Timer timer = new Timer();
private final long DELAY = 2000; // 2 seconds
int sum, amount;
FinalDTAdapter finalDTAdapter;
@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(final Editable editable) {
timer.cancel();
timer = new Timer();
timer.schedule(
new TimerTask() {
@Override
public void run() {
if (editable.toString().length() > 0) {
amount = Integer.parseInt(editable.toString());
}
GenRecNew.addn.add(amount);
System.out.println("amount :" + amount);
for (int i = 0; i < GenRecNew.addn.size(); i++) {
sum += GenRecNew.addn.get(i);
}
String res = String.valueOf(sum);
GenRecNew.mFinalDonationAmount.setText(res);
}
},
DELAY
);
}}
This code waits for 2 seconds after the user stopped typing and then processes it.