android - Add item in a ListView -
i have activity that's creating check list, if close activity button list saved in database.
the list database used mainactivity content.
this how app look:
if press list item button, new element should added list. how can add , sisplay new item (layout checkbox , edittext)?
do need adapter? don't want 'list item' part repeated.
that looks after press 4 times
activity
private arrayadapter madapter;
oncreate
listview lv = (listview) findviewbyid(r.id.my_list); list<string> initiallist = new arraylist<string>(); //load these madapter = new arrayadapter(this, android.r.id.text1, initiallist) lv.setadapter(madapter);
when event happens
madapter.add(newstring);
the add method in arrayadpater automatically take care of notifydatasetchanged()
call , update display.
textwatcher you might not need part, it's if want text added text changed question seems indicate - risky because might partial entries, recommend done button instead, in case aftertextchanged
stuff in onclick
method.
edittext edittext = (edittext) findviewbyid(r.id.my_edit_text); edittext .addtextchangedlistener(new textwatcher(){ @override public void aftertextchanged(editable arg0) { //todo: check isnt emtpy string text = arg0.gettext().tostring(); madapter.add(text); //hide add item views again arg0.settext(""); arg0.setvisibility(view.gone); } @override public void beforetextchanged(charsequence arg0, int arg1, int arg2, int arg3) { /*nothing*/ } @override public void ontextchanged(charsequence arg0, int arg1, int arg2, int arg3) { /*nothing*/ } });
Comments
Post a Comment