AutoCompleteTextView适配器没有设置

AutoCompleteTextView适配器没有设置

本文介绍了AutoCompleteTextView适配器没有设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个获取所有基于从服务器中输入的文本的建议一类。在postExecute(),我将所有的建议,我ArrayList和我想要设置的ArrayList的适配器。但它不能正常工作。

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.

本的onCreate()code:

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

当我更改文本,我可以看到服务器的响应返回数据。而在postExecute():

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

所以,建议来了ArrayList中,但它没有显示的下拉瞬间..请帮忙,感谢名单提前。

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

推荐答案

在数据(在这种情况下,ArrayList中)的变化,你需要调用 .notifyDataSetChanged()就为了适配器实例的视图重绘。

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.

这篇关于AutoCompleteTextView适配器没有设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:15