How to add text watcher on inflated view

347 views Asked by At

I m using LayoutInflater to inflate same view multiple times. i want to set TextWatcher to watch text change of a perticular view. when i use my code then its not working. please help.

LayoutInflater inflater = LayoutInflater.from(UpdateUDISENext.this);
 View inflatedLayout = inflater.inflate(R.layout.udiseupdateview, null, false);
 headerLay.addView(inflatedLayout);
 ((TextView) ((LinearLayout) headerLay.getChildAt(i)).getChildAt(0)).setText("class " + classvalue);
 totalmalestu = ((EditText) ((LinearLayout) ((LinearLayout) ((LinearLayout) headerLay.getChildAt(i)).getChildAt(1)).getChildAt(0)).getChildAt(1));
 totalmalestu.setText(value);
 totalmalestu.setTextColor(Color.parseColor("#6bb40b"));

 totalfemalestu = ((EditText) ((LinearLayout) ((LinearLayout) ((LinearLayout) headerLay.getChildAt(i)).getChildAt(1)).getChildAt(1)).getChildAt(1));
 totalfemalestu.setText(value2);
 totalfemalestu.setTextColor(Color.parseColor("#6bb40b"));
 totalstu = ((EditText) ((LinearLayout) ((LinearLayout) ((LinearLayout) headerLay.getChildAt(i)).getChildAt(1)).getChildAt(2)).getChildAt(1));
 totalstu.setTextColor(Color.parseColor("#6bb40b"));

 ((EditText) ((LinearLayout) ((LinearLayout) ((LinearLayout) headerLay.getChildAt(i)).getChildAt(1)).getChildAt(2)).getChildAt(1)).setText(String.valueOf(Integer.valueOf(value) + Integer.valueOf(value2)));

 totalmalestu.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) {

       if (totalmalestu.getText().toString().equals("")) {
                ((EditText) ((LinearLayout) ((LinearLayout) ((LinearLayout) headerLay.getChildAt(i)).getChildAt(1)).getChildAt(2)).getChildAt(1)).setText(String.valueOf(0 + Integer.valueOf(totalfemalestu.getText().toString())));

       } else if (totalfemalestu.getText().toString().equals("")) {
               ((EditText) ((LinearLayout) ((LinearLayout) ((LinearLayout) headerLay.getChildAt(i)).getChildAt(1)).getChildAt(2)).getChildAt(1)).setText(String.valueOf(Integer.valueOf(totalmalestu.getText().toString()) + 0));

       } else {
              String sum = String.valueOf(Integer.valueOf(totalmalestu.getText().toString()) + Integer.valueOf(totalfemalestu.getText().toString()));
              ((EditText) ((LinearLayout) ((LinearLayout) ((LinearLayout) headerLay.getChildAt(i)).getChildAt(1)).getChildAt(2)).getChildAt(1)).setText(sum);
       }
    }

    @Override
    public void afterTextChanged(Editable editable) { }

   });
2

There are 2 answers

0
AudioBubble On

You use same condition in 2 place

if (totalmalestu.getText().toString().equals("")) {
  ((EditText) ((LinearLayout) ((LinearLayout) ((LinearLayout) headerLay.getChildAt(i)).getChildAt(1)).getChildAt(2)).getChildAt(1)).setText(String.valueOf(0 + Integer.valueOf(totalfemalestu.getText().toString())));

}else if (totalfemalestu.getText().toString().equals("")) {
   ((EditText) ((LinearLayout) ((LinearLayout) ((LinearLayout) headerLay.getChildAt(i)).getChildAt(1)).getChildAt(2)).getChildAt(1)).setText(String.valueOf(Integer.valueOf(totalmalestu.getText().toString()) + 0));

}

First one will execute every time when totalmalestu.getText().toString().equals("")

So problem is your condition edit it it will work fine.

Suggestion

You can write code in afterTextChanged(Editable editable) rather then onTextChanged(CharSequence charSequence, int i, int i1, int i2)

@Override
public void afterTextChanged(Editable editable) {
    if (editable.toString().length()==0) {
       ((EditText) ((LinearLayout) ((LinearLayout) ((LinearLayout) headerLay.getChildAt(i)).getChildAt(1)).getChildAt(2)).getChildAt(1)).setText(String.valueOf(0 + Integer.valueOf(totalfemalestu.getText().toString())));

    }else if (editable.toString().length()<2) {
         ((EditText) ((LinearLayout) ((LinearLayout) ((LinearLayout) headerLay.getChildAt(i)).getChildAt(1)).getChildAt(2)).getChildAt(1)).setText(String.valueOf(Integer.valueOf(totalmalestu.getText().toString()) + 0));

    }else{
        String sum = String.valueOf(Integer.valueOf(totalmalestu.getText().toString()) + Integer.valueOf(totalfemalestu.getText().toString()));
        ((EditText) ((LinearLayout) ((LinearLayout) ((LinearLayout) headerLay.getChildAt(i)).getChildAt(1)).getChildAt(2)).getChildAt(1)).setText(sum);
     }
}

You can change your condition as you want... good luck ...

1
John Michael Pirie On

You can get by with a single text watcher that is then registered with the two edit texts.

TextWatcher textWatcher = new TextWatcher() {
    ... as you had it ...
};

totalmalestu.addTextChangedListener(textWatcher);
totalfemalestu.addTextChangedListener(textWatcher);