java - How to remove all views and add again? -
i creating views show events. retrieving records database , showing events based on records. events getting created now.
now want remove views when records deleted. if delete record view getting created in fragment.
i have add event activity in add , delete record. record gets deleted in database still can see view after finish(); activity.
creating views:
private void createevent(layoutinflater inflater, viewgroup dayplanview, int fromminutes, int tominutes, string title,string location,final int id) { final view eventview = inflater.inflate(r.layout.event_view, dayplanview, false); relativelayout.layoutparams layoutparams = (relativelayout.layoutparams) eventview.getlayoutparams(); container = (relativelayout) eventview.findviewbyid(r.id.container); textview tvtitle = (textview) eventview.findviewbyid(r.id.textviewtitle); if (tvtitle.getparent() != null) ((viewgroup) tvtitle.getparent()).removeview(tvtitle); if(location.equals("")) { tvtitle.settext("event : " + title); } else { tvtitle.settext("event : " + title + " (at : " + location +")"); } int distance = (tominutes - fromminutes); layoutparams.topmargin = dptopixels(fromminutes + 9); layoutparams.height = dptopixels(distance); eventview.setlayoutparams(layoutparams); dayplanview.addview(eventview); container.addview(tvtitle); // eventview.settag(tag); eventview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { = new intent(getactivity(), addeventactivity.class); editmode = true; i.putextra("editmode", editmode); int tag = 0; // tag =(int)v.gettag(); // i.putextra("tag",tag); // eventdata e =new eventdata(); // int id=0;/ id = e.getid(); i.putextra("id", id); startactivityforresult(i, 1); } }); } @override public void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); removeview(); showevents(); } public void showevents() { mdb = new eventtablehelper(getactivity()); events = mdb.getallevents("mon"); int tag = 0; (eventdata eventdata : events) { int id = eventdata.getid(); string datefrom = eventdata.getfromdate(); if (datefrom != null) { string[] times = datefrom.substring(11, 16).split(":"); minutesfrom = integer.parseint(times[0]) * 60 + integer.parseint(times[1]); } string title = eventdata.gettitle(); string location = eventdata.getlocation(); string dateto = eventdata.gettodate(); if (dateto != null) { //times = dateto.substring(11,16).split(":"); string[] times1 = dateto.substring(11, 16).split(":"); minutesto = integer.parseint(times1[0]) * 60 + integer.parseint(times1[1]); } createevent(inflater, dayplanview, minutesfrom, minutesto, title, location, id); tag++; id++; } }
tried remove view :
public void removeview() { arraylist<view> list = new arraylist<view>(); for(int i=0; i<list.size(); i++) { dayplanview.removeallviews(); } }
dayplanview viewgroup
dayplanview = (viewgroup) view.findviewbyid(r.id.hoursrelativelayout);
still can see view. whats going wrong please?
edit:
exception:
java.lang.runtimeexception: failure delivering result resultinfo{who=null, request=131073, result=-1, data=intent { cmp=com.example.siddhi.timetablelayout/.addeventactivity (has extras) }} activity {com.example.siddhi.timetablelayout/com.example.siddhi.timetablelayout.mainactivity}: java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.relativelayout.removeallviews()' on null object reference 02-05 08:02:37.871 29485-29485/com.example.siddhi.timetablelayout e/androidruntime: @ android.app.activitythread.deliverresults(activitythread.java:3574)
at : parent.removeallviews();
you don't need dayplanview.removeallviews(); in loop. , code looks loop not entered since size of list 0. need:
public void removeview() { dayplanview.removeallviews(); }
Comments
Post a Comment