Android, add() function of ArrayAdapter not working

后端 未结 3 689
栀梦
栀梦 2021-02-05 22:23

I have an ArrayAdapter (myAdapter) attached to an AutoCompleteTextView (textView) component.
Once the user presses a character I would like to populate AutoCompleteTextView\

相关标签:
3条回答
  • 2021-02-05 22:30

    Where do you call addItemsToAdapter()? Can you show us, how you have tried to add simple Strings to your Adapter?

    Edit: out of the comments the helpful code sample:

    adapter = new ArrayAdapter<Stock>(this, android.R.layout.simple_dropdown_item_1line, stocks);
    adapter.notifyDataSetChanged();
    textView.setAdapter(adapter);
    
    0 讨论(0)
  • 2021-02-05 22:38

    Create an array adapter with a vector or array like:

    ArrayAdapter(Context context, int textViewResourceId, T[] objects)
    

    By initializing your arrayadapter, you will make it listen to objects array. Do not add item to the adapter or clear the adapter, do your additions in "objects" array and also clear it. After changes on this array call

    adapter.notifyDataSetChanged();
    

    More specifically

    ArrayAdapter<YourContentType> yourAdapter = new ArrayAdapter<YourContentType> (this,R.id.OneOfYourTextViews,YourDataList);
    
    yourAdapter.notifyDataSetChanged();    
    aTextView.setText(yourAdapter.isEmpty() ? "List is empty" : "I have too many objects:)");
    

    This should be done after loading YourDataList, I checked your code, are you sure handler calls addStockItemsToAdapter() before you look your adapter is empty or not? You should also check if stocks vector has any elements in it.

    0 讨论(0)
  • 2021-02-05 22:49

    I had the exact same problem. After examining the ArrayAdapter and AutoCompleteTextView source code, I found out that the problem was, in short, that:

    • the original object list is stored in ArrayAdapter.mObjects.
    • However, AutoCompleteTextView enables ArrayAdapter's filtering, meaning that new objects are added to ArrayAdapter.mOriginalValues, while mObjects contains the filtered objects.
    • ArrayAdapter.getCount() always returns the size of mObjects.

    My solution was to override ArrayAdapter.getFilter() to return a non-filtering filter. This way mOriginalValues is null and mObjects is used instead in all cases.

    Sample code:

    public class MyAdapter extends ArrayAdapter<String> {
        NoFilter noFilter;
        /*
        ...
        */
    
        /**
         * Override ArrayAdapter.getFilter() to return our own filtering.
         */
        public Filter getFilter() {
            if (noFilter == null) {
                noFilter = new NoFilter();
            }
            return noFilter;
        }
    
        /**
         * Class which does not perform any filtering.
         * Filtering is already done by the web service when asking for the list,
         * so there is no need to do any more as well.
         * This way, ArrayAdapter.mOriginalValues is not used when calling e.g.
         * ArrayAdapter.add(), but instead ArrayAdapter.mObjects is updated directly
         * and methods like getCount() return the expected result.
         */
        private class NoFilter extends Filter {
            protected FilterResults performFiltering(CharSequence prefix) {
                return new FilterResults();
            }
    
            protected void publishResults(CharSequence constraint,
                                          FilterResults results) {
                // Do nothing
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题