【发布时间】:2010-03-01 13:33:07
【问题描述】:
我有一个附加到 AutoCompleteTextView (textView) 组件的 ArrayAdapter (myAdapter)。
一旦用户按下一个字符,我想用包含该字符的项目填充 AutoCompleteTextView 的下拉列表。
我使用 AsyncTask(使用 Web 服务)检索项目。
我调用 myAdapter.add(item) 但下拉列表为空。
我在每次添加后添加了一个调用 myAdapter.getCount() ,每次都显示为零。
调用 notifyDataSetChanged() 没有帮助。
我什至尝试添加简单的 String 对象而不是我的自定义对象,但无济于事。
我究竟做错了什么?
编辑:我按照 miette 下面的建议更改了代码,但仍然无济于事。
通常,我所做的是在我的自动完成文本视图中更改文本后,我调用一个新的 AsyncTask 并将输入的文本和一个处理程序传递给它(参见 afterTextChanged())。该任务检索与文本相关的对象,一旦完成,就会调用 Handler 的 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 a1, int a2, 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();
}
};
}
非常感谢,罗伯
【问题讨论】:
标签: android