notifyDataSetChanged example

后端 未结 5 932
再見小時候
再見小時候 2020-11-21 07:28

I\'m trying to use in my Android Application the notifyDataSetChanged() method for an ArrayAdapter but it doesn\'t work for me.

5条回答
  •  [愿得一人]
    2020-11-21 08:17

    I had the same problem and I prefer not to replace the entire ArrayAdapter with a new instance continuously. Thus I have the AdapterHelper do the heavy lifting somewhere else.

    Add this where you would normally (try to) call notify

    new AdapterHelper().update((ArrayAdapter)adapter, new ArrayList(yourArrayList));
    adapter.notifyDataSetChanged();
    
    
    

    AdapterHelper class

    public class AdapterHelper {
        @SuppressWarnings({ "rawtypes", "unchecked" })
        public void update(ArrayAdapter arrayAdapter, ArrayList listOfObject){
            arrayAdapter.clear();
            for (Object object : listOfObject){
                arrayAdapter.add(object);
            }
        }
    }
    
        

    提交回复
    热议问题