0

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) { }

   });
Karan
  • 3
  • 5
  • is `addTextChangedListener` not working? how do you know that? – pskink Sep 13 '17 at 07:45
  • i have more then 1 inflated view of same type. i want to show changes in same inflated view.. please refer ti image i have attatched. – Karan Sep 13 '17 at 07:48
  • i dont see any image.. what exactly is not working? `addTextChangedListener` ? – pskink Sep 13 '17 at 07:52
  • my layout have two edit text for male and female count. and one more is total. i want that if i change male or female edit text then it add and result sets on total. so i have used text watcher on male and female. but its not working because of more then one inflated layout of same type. – Karan Sep 13 '17 at 07:55
  • now see image descreption above – Karan Sep 13 '17 at 07:56
  • https://i.stack.imgur.com/j68YA.png – Karan Sep 13 '17 at 07:56
  • i want to change only class 2 data. – Karan Sep 13 '17 at 07:57
  • 6+2=8, 6+1=7, 17+3=20 is it wrong? – pskink Sep 13 '17 at 07:57
  • its right but when i change male or female then its not changed in total. pic given is my inital state. before using text watcher. – Karan Sep 13 '17 at 08:27
  • so you need to call `addTextChangedListener` twice: for male `EditText` and for female `EditText` – pskink Sep 13 '17 at 08:31
  • yes i have call two addTextChangedListener. for male and for female. but my layout have more then one male edit text and female edittext. and i want to get change on that perticular edittext box – Karan Sep 13 '17 at 08:42

2 Answers2

1

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);
  • i have inflated same view more then once. so in my layout i have more then one malestu and femalestu. how to differentiate between them. means how system know that text change listener is called for which malestu of that layout. – Karan Sep 13 '17 at 08:40
0

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 ...