Android notifyDataSetChanged不';不行?

Android notifyDataSetChanged不';不行?,android,Android,我写了以下申请书: 有一个AutoCompleteTextView字段 作为适配器,我将ArrayAdapter与ListArray一起使用 ListArray由一些常量字符串项和一个项组成,每当用户在字段中键入内容时,这些项都会动态更改 我使用TextChangedListener更新最后一个列表项。但这种更新似乎只发生一次 我添加了一些我自己的代码。也许有人能告诉我,我做错了什么 public class HelloListView extends Activity { Lis

我写了以下申请书:

  • 有一个AutoCompleteTextView字段
  • 作为适配器,我将ArrayAdapter与ListArray一起使用
  • ListArray由一些常量字符串项和一个项组成,每当用户在字段中键入内容时,这些项都会动态更改
我使用TextChangedListener更新最后一个列表项。但这种更新似乎只发生一次

我添加了一些我自己的代码。也许有人能告诉我,我做错了什么

public class HelloListView extends Activity 
{
    List<String> countryList = null;    
    AutoCompleteTextView textView = null;   
    ArrayAdapter adapter = null;

    static String[] COUNTRIES = new String[] 
    {
          "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",
          "Yemen", "Yugoslavia", "Zambia", "Zimbabwe", ""
    };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        

        countryList = Arrays.asList(COUNTRIES);

        textView = (AutoCompleteTextView) findViewById(R.id.edit);
        adapter = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, countryList);
        adapter.notifyDataSetChanged();        
        textView.setAdapter(adapter);
        textView.setThreshold(1);

        textView.addTextChangedListener(new TextWatcher() 
        {

            public void onTextChanged(CharSequence s, int start, int before, int count) 
            {               
                countryList.set(countryList.size()-1, "User input:" + textView.getText());                
            }

            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) 
            {               
            }

            public void afterTextChanged(Editable s) 
            {
            }
        });

        new Thread() 
        {
            public void run() 
            {
                // Do a bunch of slow network stuff.
                update();
            }
        }.start();        
    }

    private void update() 
    {
        runOnUiThread(new Runnable() 
        {
            public void run() 
            {
                adapter.notifyDataSetChanged();
            }
        });
    }
}
公共类HelloListView扩展活动
{
List countryList=null;
AutoCompleteTextView textView=null;
ArrayAdapter适配器=空;
静态字符串[]国家/地区=新字符串[]
{
“阿富汗”、“阿尔巴尼亚”、“阿尔及利亚”、“美属萨摩亚”、“安道尔”,
“也门”、“南斯拉夫”、“赞比亚”、“津巴布韦”
};
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
countryList=Arrays.asList(国家);
textView=(AutoCompleteTextView)findViewById(R.id.edit);
adapter=new ArrayAdapter(这是android.R.layout.simple\u下拉列表\u item\u 1line,countryList);
adapter.notifyDataSetChanged();
setAdapter(适配器);
设置阈值(1);
addTextChangedListener(新的TextWatcher()
{
public void onTextChanged(字符序列、int start、int before、int count)
{               
countryList.set(countryList.size()-1,“用户输入:”+textView.getText());
}
更改前的公共无效(字符序列、整数开始、整数计数、,
整数后)
{               
}
公共无效后文本已更改(可编辑)
{
}
});
新线程()
{
公开募捐
{
//做一些缓慢的网络工作。
更新();
}
}.start();
}
私有void更新()
{
runOnUiThread(新的Runnable()
{
公开募捐
{
adapter.notifyDataSetChanged();
}
});
}
}

不要修改
数组列表
。使用
add()
insert()
remove()
修改
ArrayAdapter
。您不必担心
notifyDataSetChanged()


我也同意梅拉——考虑使用<代码> AycCastase<代码>代替您的线程和<代码> RunOnUthRead()组合.

在函数<代码> OnTeXeXDE()/Cuff>中,创建一个新的适配器并附加它,然后它将工作:

countryList.set(countryList.size()-1, "User input:" + textView.getText());      
adapter = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, countryList);
textView.setAdapter(adapter);

似乎这不是一个完美的,但它的工作