android - Add new list into listView -
there has listview
, add button
in activity a. listview
data retrieved mysql
.
[![http://i.stack.imgur.com/wvmsk.png][1]][1]
when list clicked, pass data activity b, if add button
clicked, go activity b user add data. use mclickedposition
differentiate whether list
clicked or button
clicked.
activity a
listview listviewupdate; listadapter adapter; button add; string id, id; public static final int project_request_code = 1; public static final int camera_request_code = 2; int mclickedposition; string receiveproject, receivedescription, receivetimein, receivetimeout; integer receiveprogress; arraylist<detailsbean> results = new arraylist<detailsbean>(); string myjson; jsonarray details = null; arraylist<hashmap<string, string>> editdetails; listviewupdate.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> listview, view view, int position, long id) { mclickedposition = position; // listview click hashmap<string, string> clickeditem = editdetails.get(position); id = clickeditem.get(configs.tag_id); intent intent = new intent(getactivity(), edit_details.class); intent.putextra("id", id); intent.putextra("id", id); intent.putextra("mclickedposition",mclickedposition); //toast.maketext(getactivity(),"this is"+id+id,toast.length_long).show(); startactivityforresult(intent, project_request_code); } }); add.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { mclickedposition = -1; // if button clicked intent intent = new intent(getactivity(), edit_details.class); intent.putextra("id", id); startactivity(intent); } });
assume user click add button
.(mclickedposition==-1)
activity b
when save button
clicked, data inserted mysql
since mclickedposition==-1 after that, want new data return activity , adding new list in a, mean activity should have 2 list now. how can achieve ?
save.setonclicklistener(new view.onclicklistener() { // if save button clicked @override public void onclick(view v) { intent returnintent = new intent(); project1 = project2.getselecteditem().tostring(); description = description.gettext().tostring(); progress = seekbar.getprogress(); timein = timein.gettext().tostring(); timeout = timeout.gettext().tostring(); if(mclickedposition==-1) { add(project1,description,progress,timein,timeout); } else { update(project1, description, progress, timein, timeout); } returnintent.putextra("project1", project1); returnintent.putextra("description", description); returnintent.putextra("progress", progress); returnintent.putextra("timein", timein); returnintent.putextra("timeout", timeout); setresult(activity.result_ok, returnintent); finish(); } });
activity onactivityresult
@override public void onactivityresult(int requestcode, int resultcode, intent data) { // receive activity b , populate listview if (resultcode == activity.result_ok) { if (requestcode == project_request_code) { receiveproject = data.getstringextra("project1"); receivedescription = data.getstringextra("description"); receiveprogress = data.getintextra("progress", 0); receivetimein = data.getstringextra("timein"); receivetimeout = data.getstringextra("timeout"); toast.maketext(getactivity(),receiveproject+receivedescription+receiveprogress+receivetimein+receivetimeout,toast.length_long).show(); if(mclickedposition==-1) { // add list hashmap<string, string> clickeditem = new hashmap<>(); clickeditem.put(configs.tag_project, receiveproject); clickeditem.put(configs.tag_workdescription, receivedescription); clickeditem.put(configs.tag_percentage, receiveprogress + ""); clickeditem.put(configs.tag_in, receivetimein); clickeditem.put(configs.tag_out, receivetimeout); editdetails.add(clickeditem); ((baseadapter) adapter).notifydatasetchanged(); } else { // update list hashmap<string, string> clickeditem =editdetails.get(mclickedposition); clickeditem.put(configs.tag_project, receiveproject); clickeditem.put(configs.tag_workdescription, receivedescription); clickeditem.put(configs.tag_percentage, receiveprogress + ""); clickeditem.put(configs.tag_in, receivetimein); clickeditem.put(configs.tag_out, receivetimeout); ((baseadapter) adapter).notifydatasetchanged(); } //((baseadapter) adapter).notifydatasetchanged(); } } } }
now can see new list added in activity a. when click new list , go activity b, no data passed. if click old list, can see data passed...
you need add or update new data in list , call notifidatasetchanged()
; change code in onactivity result method of activity below-
hashmap<string, string> item = new hashmap<>(); item.put(configs.tag_project, receiveproject); item.put(configs.tag_workdescription, receivedescription); item.put(configs.tag_percentage, receiveprogress + ""); item.put(configs.tag_in, receivetimein); item.put(configs.tag_out, receivetimeout); if(mclickedposition==-1) { // add list editdetails.add(item); } else { // update list editdetails.remove(mclickedposition); meventslist.add(mclickedposition,item) } ((baseadapter) adapter).notifydatasetchanged();
Comments
Post a Comment