Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid, add() function of ArrayAdapter not working
    text
    copied!<p>I have an ArrayAdapter (myAdapter) attached to an AutoCompleteTextView (textView) component.<br> Once the user presses a character I would like to populate AutoCompleteTextView's drop down list with items containing this character.<br> I retrieve the items using AsyncTask (which uses a web service).</p> <p>I call myAdapter.add(item) but the drop down list is empty.<br> I added a call myAdapter.getCount() after each addition and it shows zero every time. Calling notifyDataSetChanged() didn't help.<br> I even tried to add simple String objects instead of my custom objects, to no avail.<br> What am I doing wrong? </p> <p><strong>Edit:</strong> I changed the code as miette suggested below but still to no avail.<br> Generally, what I do is after text is changed in my auto complete text view, I call a new AsyncTask and pass it the entered text and a Handler (see afterTextChanged()). The task retrieves objects relevant to the text and once done the Handler's handleMessage() is called. In handleMessage() I attempt to populate the adapter's objects. But still the adapter's drop down list ends up empty.</p> <p>Here is my code: </p> <pre><code>public class AddStockView extends Activity implements OnClickListener, OnItemClickListener, TextWatcher { ArrayAdapter&lt;Stock&gt; adapter; AutoCompleteTextView textView; Vector&lt;Stock&gt; stocks; public AddStockView() { // TODO Auto-generated constructor stub stocks = new Vector&lt;Stock&gt;(); } 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&lt;Stock&gt;(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&lt;?&gt; 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&lt;Object&gt; dataItems) { for (int i = 0; i &lt;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(); } }; } </code></pre> <p>Thanks a lot, Rob</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload