Java – replace the list of arrayadapters

I have a custom arrayadapter that uses ArrayList < orderedproductitem > For my listview In this listview, I have an AutoCompleteTextView, which also uses a custom arrayadapter with ArrayList < Product > During initialization, this AutoCompleteTextView contains the names of all products (about 1250), minus 1 (products of orderedproductitem)

Then, the user can use spinner to select product tag and create a filtered ArrayList < Product >, so I only get a list of all products containing this product tag (still minus 1) So my question is: what is the best way to replace the arrayadapter object list?

I know that I can recreate and reassign a new arrayadapter instance in listactivity:

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

I also know that I can use clear and then use addall to replace everything:

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

I think the second is probably the best, because we don't want to create a new instance every time, or does the arrayadapter have some method, such as setarray (), which I missed when looking for it? Or is there another way to perform better than the second option?

Solution

Here, you want to change the data of the customadapter and create a public method in the adapter

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

Now, whenever you want to update data, use the following code:

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

There is no need to re instantiate your adapter

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>