Android, add() function of ArrayAdapter not working

后端 未结 3 741
栀梦
栀梦 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: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 yourAdapter = new ArrayAdapter (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.

提交回复
热议问题