Dynamic addition and deletion of Android--listview

Source: Internet
Author: User


One, add

Case r.id.imgbtnadd:/* Add list */if (Settemplistview.getchildcount () >5) {Toast.maketext (Getapplicationcontext (), " List can only add 6! ", Toast.length_short). Show (); break;} Listitem.clear (); for (int i=0;i<settemplistview.getchildcount (); i++) {View view=settemplistview.getchildat (i); TextView txttemp= (TextView) View.findviewbyid (r.id.txtbegintime); EditText edittemp= (EditText) View.findviewbyid (r.id.txttemperature); Hashmap<string,string> map=new hashmap<string,string> ();      Map.put ("BeginTime", Txttemp.gettext (). toString ());    Map.put ("Txttemperature", Edittemp.gettext (). toString ());    Listitem.add (map);} Hashmap<string,string> map1=new hashmap<string,string> ();      Map1.put ("BeginTime", "-Please Select Time-");    Map1.put ("Txttemperature", "" ");    Listitem.add (MAP1);    Listitemadapter.notifydatasetchanged (); break;


Two, delete

ListItemView.imgDelete.setOnClickListener (New Onclicklistener () {@Overridepublic void OnClick (View v) {// Toast.maketext (Context, mposition+ "", Toast.length_short). Show (); Listitem.remove (mposition); notifydatasetchanged ();}});


third, the modified GetView method


Public View getView (final int mposition, View Convertview, ViewGroup parent) {//final int mposition = position; Listitemview Listitemview = null; if (Convertview = = null) {Convertview = listcontainer.inflate (resource, null);//Load Layout Listitemview = new Listitemview () ;/* Initialize Control Container collection */listitemview.txtbegintime= (TextView) Convertview.findviewbyid (r.id.txtbegintime); /* Start time */listitemview.txttemperature= (EditText) Convertview.findviewbyid (r.id.txttemperature);/* Temperature display */ Listitemview.imgdelete= (ImageView) Convertview.findviewbyid (r.id.imgdelete);/* Delete this line *///set the control set to Convertview// Convertview.settag (Listitemview);//}else{//listitemview= (Listitemview) Convertview.gettag ();//Using Cached view//} ListItemView.txtBeginTime.setText (Listitem.get (mposition). Get ("BeginTime")); ListItemView.txtTemperature.setText (Listitem.get (mposition). Get ("txttemperature"));// ListItemView.txtBeginTime.setText (Listitem.get (mposition). Get ("BeginTime"));/* Initialization of the control */new Popdatetimepicker ( Listitemview.txtbegintime);/* Bind TextChange event, Mode GetViewDiscard Data */listitemview.txtbegintime.addtextchangedlistener when initializing (new Textwatcher () {@Overridepublic void Aftertextchanged (Editable s) {listitem.get (mposition). put ("BeginTime", s.tostring ());} @Overridepublic void Beforetextchanged (charsequence s, int start, int count,int after) {} @Overridepublic void Ontextchang Ed (charsequence s, int start, int before,int count) {}}); ListItemView.txtTemperature.addTextChangedListener (new Textwatcher () {@Overridepublic void aftertextchanged (Editable s) {listitem.get (mposition). put ("Txttemperature", S.tostring ());} @Overridepublic void Beforetextchanged (charsequence s, int start, int count,int after) {} @Overridepublic void Ontextchang Ed (charsequence s, int start, int before,int count) {}}); ListItemView.imgDelete.setOnClickListener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//toast.maketext (context, mposition+ "", Toast.length_short). Show (); Listitem.remove (mposition); notifydatasetchanged ();}); return Convertview;}


The activity is as follows:







Alas, really feel the ListView is very wonderful (more than one screen of the problem has not been disposed of, have experience to leave a message).







Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Dynamic addition and deletion of Android--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.