1

Aim :

I want to get some strings from webservice and populate AutoCompleteTextView with them.This is simple but what I want actually is to begin search (invoke webservice) when typing finished.

I mean, for example, I type something and 3 seconds after typing finished, AutoCompleteTextView would get populated and suggestions show up.

What I did so far :

As you can see in the code below, I used a CountDownTimer to achieve this. I set it 3 seconds and start it in OnTextChanged. When user type, I clear CountDownTimer and create a new instance of it and start it again.

So, whenever user type -on every key press- I reset counter.

After that, I call my method - which invoke webservice and populate AutoCompleteTextView - in CountDownTimer's OnFinish().

Problem :

When I finish typing, everything works as expected as I can see in debug mode. But suggestions doesn't come up FOR ONLY FIRST SEARCH.

I mean, it works as well but AutoCompleteTextView doesn't get populated with data for only FIRST TIME.

Note :

I invoke webservice both synchronous and asynchronous way.

counter=new MyCount(3000, 1000);
autoComplete.addTextChangedListener(new TextWatcher(){

        public void afterTextChanged(Editable editable) {
            
            if(isBackPressed){  //catch from KeyListener, if backspace is pressed don't invoke web service
                
                isBackPressed=false;
                return;
            }
                
            String newText = editable.toString();
            
            searchText=newText;
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {


        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {

            if(isBackPressed){
                
                isBackPressed=false;
                return;
            }
                
            counter.cancel();
            counter=new MyCount(3000, 1000); 
            counter.start();
        }

    });

Here is my CountDownTimer Class

    public class MyCount extends CountDownTimer{

    public MyCount(long millisInFuture, long countDownInterval) {

    super(millisInFuture, countDownInterval);

    }
    @Override
    public void onFinish() {
        
        invoke_webservice(searchText);
    }
    @Override
    public void onTick(long millisUntilFinished) {
        
    }
    }

Here is my method to invoke webservice in synchronous way

public void invoke_webservice(String key){
    
    try{
                    . //code to invoke webservice and populate string [] results
                    .
                    .
    aAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.item,results);
    autoComplete.setAdapter(aAdapter);
    aAdapter.notifyDataSetChanged();
    
}

Here is my method to invoke webservice in asynchronous way

class getJson extends AsyncTask<String,String,String>{

    @Override
    protected String doInBackground(String... key) {
        
        String newText = key[0];

                    . //code to invoke webservice and populate string [] results
                    .
                    .

        
        runOnUiThread(new Runnable(){
            public void run(){
                
                aAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.item,results);
                autoComplete.setAdapter(aAdapter);
                aAdapter.notifyDataSetChanged();
            }
        });
        
        return null;
    }

}

Thanks in advance

Community
  • 1
  • 1
sadeceerem
  • 19
  • 3
  • please don't add duplicate posts for same problem, merge your posts so people don't waste their time looking and answering both posts. This usually considered as spamming http://stackoverflow.com/questions/10143308/android-autocompletetextview-filter-is-firing-late-for-the-first-search-only – Mayank Apr 14 '12 at 08:35
  • @Mayank the link above is not working...please check – Snehal Poyrekar Sep 06 '12 at 07:29
  • I think user have deleted his post that's why the link is not working anymore. He had same question in another post, so I just pointed out to him that it is not a good idea to do that and can be considered spamming. – Mayank Sep 07 '12 at 06:25

1 Answers1

0

Here is what you can do: i have set the threshold value to 3

 atvSearchPatient.setThreshold(3);

apply the text watcher listener on autocomplete text view like below:

 //here set the text watcher
    atvSearchPatient.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            String str = atvSearchPatient.getText().toString().trim();
            if (str.length() >2) {

                    searchPatient(str );

            } 
        }
    });

on your first call hit API with null values and for second and consecutive calls with string from autocomplete textview field as above.and then in response apply the adapter like below:

this.nameList.clear();
    if (nameList.size() > 0) {
        this.nameList.addAll(dataFromResponseList);
        atvSearchPatient.setAdapter(null);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(
                this, android.support.v7.appcompat.R.layout.select_dialog_item_material, nameList);
        atvSearchPatient.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        atvSearchPatient.showDropDown();

It's weired problem for first time not working but that's the only way working in my case nothing else. hope it helps Thanks

vikas kumar
  • 10,447
  • 2
  • 46
  • 52