Search code examples
javaandroidarraylistandroid-arrayadapternotifydatasetchanged

Replace List of ArrayAdapter


I have a custom ArrayAdapter that uses an ArrayList<OrderedProductItem> for my ListView. Inside this ListView I have an AutoCompleteTextView that also uses a custom ArrayAdapter with an ArrayList<Product>. On initialization this AutoCompleteTextView contains the Names of all Products (around 1250), minus 1 (the OrderedProductItem's Product).

The User can then use a Spinner to select a Product-Tag and I create a filtered ArrayList<Product>, so I only get a list of all Products containing this Product-Tag (still minus the 1). So, my question is: What is the best way to replace an ArrayAdapter's object-list?

I know I can just re-create and re-assign a new ArrayAdapter instance in my ListActivity:

myAdapter = new MyAdapter(this, R.layout.item_view, myList);
setListAdapter(myAdapter);

I also know I can use a clear and then an addAll to replace everything:

// NOTE: myAdapter.setNotifyOnChange is kept on default (true)
myAdapter.clear();
myAdapter.addAll(myList);

I guess the second one is probably best, since we don't want to create a new Instance every time, or does ArrayAdapter have some kind of method like setArray() and I just missed it when looking for it? Or is there even another way that is better for performance than this second option?


Solution

  • Here you want to change data of customAdapter make a public method in adapter

    public void updateData(ArrayList<T> list){
     mAdapterProductList=list;
    }
    

    and now whenever u want to update data use these codes:

    adpter.updateData(mUpdatedList);
    adapter.notifyDataSetChanges();
    

    No need to re-instantiate your adapter