"Android Notes" Arrayadapter Delete Item considerations

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.