我写下面的申请:
我使用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();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
Com*_*are 11
不要修改ArrayList.修改ArrayAdapter,使用add(),insert()和remove().你不用担心notifyDataSetChanged().
另外,我同意Mayra - 考虑使用AsyncTask而不是你的线程和runOnUiThread()组合.
| 归档时间: |
|
| 查看次数: |
8194 次 |
| 最近记录: |