Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suggestions not shown in AutoCompleteTextView

I have a AutoCompleteTextView in my layout. When a user enter "@" character in that i have to show them some suggestions. It normally names i get it from internet.

I am getting the names and i create an ArrayAdapter as shown below.

autoCtextView.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub
            String lsatChar = s.toString().substring(s.length()-1,s.length()); 
            if(lsatChar.equals("@")) {
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(DisplayQuestionDetails.this, 
                         android.R.layout.simple_list_item_1, namesLsist);
                autoCtextView.setAdapter(adapter);
            }


        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    });

But the suggestions are not shown. Am i doing anything wrong ? Please ask if need clarification on question

like image 330
user965071 Avatar asked Jan 21 '26 09:01

user965071


1 Answers

Do you miss autoCtextView.setThreshold(1); ?

(to start working from first character)

for example demo:

String[] strList={"a","aaa","aabb","b","bbc","cbb","c","cdd","caa","d","ddc","dda","e","eea","ebc","aec"};  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  

        //Creating the instance of ArrayAdapter containing list
           ArrayAdapter<String> adapter = new ArrayAdapter<String>  
            (this,android.R.layout.select_dialog_item,strList);  

        //Getting the instance of AutoCompleteTextView  
           AutoCompleteTextView autoCtextView= (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);  
           autoCtextView.setThreshold(1);         //to start working from first character  
           autoCtextView.setAdapter(adapter);//set the adapter data to the AutoCompleteTextView  




}  
like image 60
lynndragon Avatar answered Jan 23 '26 22:01

lynndragon