0

I am using text watcher on an edit text and that edit text is inside the list view. I have to call an method while edit text length == 3. But the text watcher events are executing more then one time. Here is my code :

 holder.etTypeKeyword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                //System.out.println("KEYWORD TYPED : "+s+ " count : "+count);  
                }    
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }    
            @Override
            public void afterTextChanged(Editable s) {
                if(s.length()==3){
        //          isServiceCall=true;
                    System.out.println("CALLING COUNT : " + position + " Length "+holder.etTypeKeyword.getText());
                    obKeywordSuggestionInterface.getKeywordUsingChar(s.toString(),position);

                }else{
        //          isServiceCall=false;
                }
            }
        });

Here is my log output where count message is repeating

>I/System.out: CALLING COUNT : 2 Length 
I/System.out: CALLING COUNT ACT : 2 Length Fun
I/System.out: CALLING COUNT : 2 Length 
I/System.out: CALLING COUNT ACT : 2 Length Fun
I/System.out: CALLING COUNT : 2 Length 
I/System.out: CALLING COUNT ACT : 2 Length Fun
I/System.out: CALLING COUNT : 2 Length 
I/System.out: CALLING COUNT ACT : 2 Length Fun
I/System.out: CALLING COUNT : 2 Length 
I/System.out: CALLING COUNT ACT : 2 Length Fun
I/System.out: CALLING COUNT : 2 Length 
I/System.out: CALLING COUNT ACT : 2 Length Fun
I/System.out: CALLING COUNT : 2 Length 
I/System.out: CALLING COUNT ACT : 2 Length Fun
I/System.out: CALLING COUNT : 2 Length 
I/System.out: CALLING COUNT ACT : 2 Length Fun
I/System.out: CALLING COUNT : 2 Length 
I/System.out: CALLING COUNT ACT : 2 Length Fun

Please some body please help me on this to restrict multiple execution of text watcher events. Thanks in advance.

Harry
  • 369
  • 2
  • 17
  • From where is 'CALLING COUNT ACT' getting logged? – Shaishav Aug 12 '16 at 06:29
  • That is because it is in a listView or recyclerView. When you scroll, the method will be called for each item in the list. – Akshay Bhat 'AB' Aug 12 '16 at 06:33
  • @Shaishav. It is a List view. I am not scrolling. just typing characters within edit text and method **obKeywordSuggestionInterface.getKeywordUsingChar(s.toString(),position);** called multiple times. – Harry Aug 12 '16 at 06:35
  • @Akshay, Please see this link which has same problem [http://stackoverflow.com/questions/33375484/textwatcher-ontextchanged-called-multiple-times] – Harry Aug 12 '16 at 06:41
  • But What do you want to happen from the code? – Akshay Bhat 'AB' Aug 12 '16 at 07:29

1 Answers1

0

Please make sure you are not setting text on edittext inside obKeywordSuggestionInterface.getKeywordUsingChar

Ramit
  • 416
  • 3
  • 8