android - Deleting last item from spinner deletes the entire list -
i trying use spinner control enable user delete list element. have 'add' button add elements list, , 'delete' button removes currently-displayed item list.
it works expected except when user deletes last item in list. @ point, of list's items deleted.
my code follows:
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); // grab our ui elements can manipulate them (for spinner) // or add listeners them (in case of buttons) m_mydynamicspinner = (spinner)findviewbyid(r.id.dynamicspinner); m_additemtext = (edittext)findviewbyid(r.id.newspinneritemtext); button addbutton = (button)findviewbyid(r.id.addbtn); button clearbutton = (button)findviewbyid(r.id.clearbtn); // create arrayadapter assign spinner m_adapterforspinner = new arrayadapter(this, android.r.layout.simple_spinner_item); ((arrayadapter)m_adapterforspinner).setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); m_mydynamicspinner.setadapter(m_adapterforspinner); // add listener addbutton addbutton.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { addnewspinneritem(); } }); clearbutton.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { clearspinneritems(); } }); } // add listener addbutton private void addnewspinneritem() { if (m_additemtext.gettext().length() == 0) { toast.maketext(getapplicationcontext(), "the textview empty", toast.length_long).show(); } else { charsequence textholder = "" + m_additemtext.gettext(); ((arrayadapter) m_adapterforspinner).add(textholder); } m_additemtext.settext(""); } private void clearspinneritems() { m_mydynamicspinner.setonitemselectedlistener(new onitemselectedlistener() { @override public void onitemselected(adapterview<?> parent, view view, int pos, long id) { object t = m_adapterforspinner.getitem(pos); ((arrayadapter) m_adapterforspinner).remove((charsequence) t); } @override public void onnothingselected(adapterview<?> arg0) { // todo } }); }
does have ideas or suggestions on how make work?
the problem code deletion inside onitemselected callback, gets called every time deleting entry, deleting recursively until not have more entries select. if add log inside method:
log.d("spinner", "count: " + m_adapterforspinner.getcount());
you see mean. i'm sure can come more elegant code, quick , dirty hack set boolean flag stop recursion after first deletion. see snippet below , add commented lines own code:
public class spinnertest extends activity { spinner m_mydynamicspinner; edittext m_additemtext; arrayadapter m_adapterforspinner; public static boolean cleared = false; // <--- set static boolean here @override public void oncreate(bundle savedinstancestate) { // code unchanged clearbutton.setonclicklistener(new onclicklistener() { public void onclick(view v) { cleared=false; // <--- nope, did not clear value yet clearspinneritems(); } }); } // code unchanged private void clearspinneritems() { m_mydynamicspinner.setonitemselectedlistener(new onitemselectedlistener() { public void onitemselected(adapterview<?> parent, view view, int pos, long id) { object t = m_adapterforspinner.getitem(pos); log.d("spinner", "count: " + m_adapterforspinner.getcount()); if (!cleared) // <--- did already? ((arrayadapter) m_adapterforspinner).remove((charsequence) t); log.d("spinner", "count: " + m_adapterforspinner.getcount()); cleared=true; // did it! } // code unchanged
Comments
Post a Comment