Element deletion of listview in Android

Source: Internet
Author: User

If a listview is written, each row has a delete button to delete the row. The click event of the delete button is implemented in the adapter.

@ Override <br/> Public void onclick (view v) {<br/> button delbutton = (button) V; <br/> String title = (string) delbutton. gettag (); <br/> preference. getallviews (). remove (title); <br/> mtitlist. remove (title); <br/> baseadapter. notifydatasetchanged (); <br/>}

The getview method is called again.

@ Override <br/> Public View getview (INT position, view convertview, viewgroup parent) {<br/> linearlayout layout = NULL; <br/> tictactoenode node = NULL; <br/> If (position = 0) {<br/> node = new tictactoenode (); <br/> node. settitle ("Homepage"); <br/>}< br/> else {<br/> node = preference. getnodebytitle (mtitlist. get (position); <br/>}< br/> // If (convertview = NULL) {</P> <p> layoutinflater Inflater = layoutinflater. from (mcontext); <br/> layout = (linearlayout) Inflater. inflate (R. layout. windows_manage_list_item, null); </P> <p> imageview icon = (imageview) layout. findviewbyid (R. id. item_icon); <br/> If (position = 0) {<br/> icon. setimagebitmap (bitmapfactory. decoderesource (mcontext. getresources (), R. drawable. icon); <br/>}< br/> else {<br/> bitmap iconbitmap = resourceutil. getimagebysd (preference. sdcard_url + node. getIcon (); <br/> icon. setimagebitmap (iconbitmap); <br/> // icon. setimagebitmap (preference. getinstence (). getallviews (). get (node. gettitle ()). getdrawingcache (); <br/>}</P> <p> textview titleview = (textview) layout. findviewbyid (R. id. item_name); <br/> titleview. settext (node. gettitle (); <br/> titleview. settextsize (14); <br/> button deletebutton = (button) layout. findviewbyid (R. id. del_button); <br/> If (position = 0) {<br/> deletebutton. setvisibility (view. invisible); <br/>}< br/> else {<br/> deletebutton. setvisibility (view. visible); <br/> deletebutton. setonclicklistener (deletebuttonlistener); <br/> deletebutton. setfocusable (false); <br/> deletebutton. settag (node. gettitle (); <br/>}</P> <p> //} <br/> // else {<br/> // layout = (linearlayout) convertview; <br/>/}</P> <p> return layout; <br/>}

Because of the reuse mechanism, a new row view is created only when convertview is null. The actual result is that the first row element is deleted (only the view is disordered and the data is correct ), I think removeview should be used to delete this view to prevent the above problems. But how can we get this view?

Method to verify: Write down the index of the row based on the tag of the button, and delete the view from the passed listview.

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.