Arrayadapter provides the Remove method to delete data from the data source and to refresh the interface. Here is its source code:
public void Remove (T object) { synchronized (mLock) { if (moriginalvalues! = null) { Moriginalvalues.remove ( object); } else { Mobjects.remove (object); } } if (Mnotifyonchange) notifydatasetchanged ();//Notify Observer }
Here is an example of arrayadapter use:
Mlistview = (ListView) Findviewbyid (r.id.lv); string[] data = {"Aaaaaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb", "aaaaaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb", " Aaaaaaaaaaaaaaaaaa "," bbbbbbbbbbbbbbbb "," aaaaaaaaaaaaaaaaaa "," bbbbbbbbbbbbbbbb ",};final List<String> d = new Arraylist<string> (arrays.aslist (data)) Madapter = new Arrayadapter<string> (this, R.layout.item, R.id.tv , d); Mlistview.setadapter (Madapter); Mlistview.setonitemclicklistener (new Onitemclicklistener () {@Overridepublic void Onitemclick (int postion) {madapter.remove (Madapter.getitem (postion));}});
There is no problem with the above code, you can delete entries. But when we construct the Arrayadapter, we pass in the
If the data source is not a list type but an array type, then the deletion will be an error!
Reason:To view the Arrayadapter constructor:
Public Arrayadapter (context context, int resource, int textviewresourceid, t[] objects) { init (context, resource, Tex Tviewresourceid, Arrays.aslist (objects));}
As you can see, it calls the Arrays.aslist method to convert the array to the list type, but we know that the list cannot be add/remove, so the Arrayadapter remove method will give an error. So, when you construct Arrayadapter, it's best to pass in a list collection (modifiable), or you can:
List<string> d = new arraylist<string> (arrays.aslist (data));
"Android Notes" Arrayadapter Delete Item considerations