AutoCompleteTextView adapter not set

被刻印的时光 ゝ 提交于 2019-12-13 06:38:36

问题


I have a class that fetches all the suggestions based on the text entered from the server. In the postExecute(), I am adding all the suggestions to my ArrayList and I want to set that arraylist as adapter. But it is not working.

The onCreate() code:

     t1 = (AutoCompleteTextView) 
            findViewById(R.id.autoCompleteTextView1);



    t1.setThreshold(1);
    t1.addTextChangedListener(new TextWatcher() {

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

           //DoPost() is the class fetching data from server
            new DoPOST().execute("");

        }

        @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

        }
    });

    ArrayAdapter<String> adp=new ArrayAdapter<String>(this,
            android.R.layout.simple_dropdown_item_1line,Names);
    t1.setAdapter(adp);

When I change the text, I can see the server response returning the data. And in postExecute():

for(int i=0 ;i<js.length();i++){
            try {
                JSONObject tokenobj=js.getJSONObject(i);
                Names.add(tokenobj.get("suggestion").toString());
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
          }

So the suggestions are coming in the arraylist but it is not showing as the dropdown instantly.. Please help, Thanx in advance.


回答1:


When the data (the ArrayList in this case) changes, you need to call .notifyDataSetChanged() on the adapter instance in order for the view to redraw.



来源:https://stackoverflow.com/questions/30367712/autocompletetextview-adapter-not-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!