未设置AutoCompleteTextView适配器

时间:2015-05-21 07:52:16

标签: android android-activity autocompletetextview

我有一个类根据从服务器输入的文本获取所有建议。在postExecute()中,我将所有建议添加到我的ArrayList中,并且我想将该arraylist设置为适配器。但它没有用。

onCreate()代码:

     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);

当我更改文本时,我可以看到服务器响应返回数据。在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();
            }
          }

所以这些建议都会出现在arraylist中,但它并没有立即显示为下拉列表。 请帮忙, 提前完成。

1 个答案:

答案 0 :(得分:1)

当数据(在这种情况下为ArrayList)发生更改时,您需要在适配器实例上调用.notifyDataSetChanged()以便重绘视图。

相关问题