【问题标题】:AutoCompleteTextView adapter not set未设置 AutoCompleteTextView 适配器
【发布时间】:2015-05-21 15:55:39
【问题描述】:

我有一个类可以根据从服务器输入的文本获取所有建议。在 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();
            }
          }

所以建议出现在数组列表中,但它没有立即显示为下拉列表.. 请帮忙, 提前谢谢。

【问题讨论】:

标签: android android-activity autocompletetextview


【解决方案1】:

当数据(本例中为 ArrayList)发生变化时,您需要在适配器实例上调用 .notifyDataSetChanged() 以重绘视图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-26
    • 2015-03-28
    • 1970-01-01
    • 2017-05-09
    • 1970-01-01
    • 2018-08-12
    • 2017-02-28
    相关资源
    最近更新 更多