我写了以下申请书:
有一个AutoCompleteTextView字段
作为适配器,我正在使用带有listarray的arrayadapter
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();
}
});
}
}
最佳答案
不要修改ArrayList
。使用ArrayAdapter
、add()
和insert()
修改remove()
。您不必担心notifyDataSetChanged()
。
另外,我同意mayra——考虑使用AsyncTask
而不是您的thread和runOnUiThread()
组合。