Android notifyDataSetChanged不';不行?
我写了以下申请书:Android notifyDataSetChanged不';不行?,android,Android,我写了以下申请书: 有一个AutoCompleteTextView字段 作为适配器,我将ArrayAdapter与ListArray一起使用 ListArray由一些常量字符串项和一个项组成,每当用户在字段中键入内容时,这些项都会动态更改 我使用TextChangedListener更新最后一个列表项。但这种更新似乎只发生一次 我添加了一些我自己的代码。也许有人能告诉我,我做错了什么 public class HelloListView extends Activity { Lis
- 有一个AutoCompleteTextView字段
- 作为适配器,我将ArrayAdapter与ListArray一起使用
- ListArray由一些常量字符串项和一个项组成,每当用户在字段中键入内容时,这些项都会动态更改
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);
似乎这不是一个完美的,但它的工作不确定为什么你的工作不起作用,但是你是否考虑过使用异步任务来完成你的网络工作?它为您提供了一种在另一个线程上进行工作的简单方法,然后在UI线程上进行一些工作。但如果你看看我的例子,根本就没有网络的东西。我有ArrayList的数据已经在代码中。尝试你的建议。它不起作用,请看我的答案:我有一个建议使用stackoverflow的runOnUIThread。在我的例子中,添加、插入和删除操作不是很好。我只想要一件,我会换的。