我有一个附加到AutoCompleteTextView(textView)组件的ArrayAdapter(myAdapter).
一旦用户按下字符,我想用包含此字符的项目填充AutoCompleteTextView的下拉列表.
我使用AsyncTask(使用Web服务)检索项目.
一旦用户按下字符,我想用包含此字符的项目填充AutoCompleteTextView的下拉列表.
我使用AsyncTask(使用Web服务)检索项目.
我调用myAdapter.add(item),但是下拉列表是空的.
每次添加后,我添加了一个myAdapter.getCount()函数,每次都显示为零.
调用notifyDataSetChanged()没有帮助.
我甚至试图添加简单的String对象而不是我的自定义对象,没有用.
我究竟做错了什么?
编辑:我将代码改为下面提到的miette,但仍然无济于事.
一般来说,我做的是在自动完成文本视图中更改文本后,我调用一个新的AsyncTask并传入输入的文本和处理程序(参见afterTextChanged()).任务检索与文本相关的对象,并且一旦完成了处理程序的handleMessage()被调用.在handleMessage()中,我尝试填充适配器的对象.但是适配器的下拉列表仍然为空.
这是我的代码:
public class AddStockView extends Activity implements OnClickListener,OnItemClickListener,TextWatcher { ArrayAdapter<Stock> adapter; AutoCompleteTextView textView; Vector<Stock> stocks; public AddStockView() { // TODO Auto-generated constructor stub stocks = new Vector<Stock>(); } public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.add_stock_view); findViewById(R.id.abort_button).setOnClickListener(this); adapter = new ArrayAdapter<Stock>(this,android.R.layout.simple_dropdown_item_1line,stocks); //adapter.setNotifyOnChange(true); textView = (AutoCompleteTextView) findViewById(R.id.search_edit_text); textView.setAdapter(adapter); textView.setOnItemClickListener(this); textView.addTextChangedListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.abort_button: finish(); break; case R.id.search_edit_text: break; } } @Override public void onItemClick(AdapterView<?> parent,View v,int position,long id) { // TODO Auto-generated method stub Stock stockToAdd = (Stock)parent.getAdapter().getItem(position); //TODO: Add the above stock to user's stocks and close this screen finish(); } @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); getMenuInflater().inflate(R.layout.menu,menu); CategoryMenu.getInstance().populateMenu(menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { CategoryMenu.getInstance().menuItemSelected(item,this); return false; } @Override public boolean onPrepareOptionsMenu(Menu menu) { return true; } @Override public void afterTextChanged(Editable text) { // TODO Auto-generated method stub if (text.toString().equals("")) return; new AppTask().execute(new AppTask.Payload(Consts.taskType.SEARCH_STOCK,new Object[] {text,handler},this)); } @Override public void beforeTextChanged(CharSequence a0,int a1,int a2,int a3) { // TODO Auto-generated method stub } @Override public void onTextChanged(CharSequence a0,int a3) { // TODO Auto-generated method stub } private void addStockItemsToAdapter(Vector<Object> dataItems) { for (int i = 0; i <dataItems.size(); i++) { Stock stk = (Stock)dataItems.elementAt(i); stocks.add(stk); } } public void populateAdapter() { addStockItemsToAdapter(ContentReader.getInstance.getDataItems()); adapter.notifyDataSetChanged(); int size = adapter.getCount(); // size == 0 STILL!!!! textView.showDropDown(); } final Handler handler = new Handler() { public void handleMessage(Message msg) { populateAdapter(); } }; }
非常感谢Rob