android - how to destroy view object that i created earlier -
i want destroy object popupview object of view class created in onclicklistener.
i want mechanism such every time should create new view. having problem way creating view object.
my total code:
package com.addictioncounterapp; import java.text.parseexception; import java.text.simpledateformat; import java.util.calendar; import java.util.date; import android.app.activity; import android.app.alertdialog; import android.app.datepickerdialog; import android.app.dialog; import android.app.alertdialog.builder; import android.content.dialoginterface; import android.content.intent; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.os.bundle; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.datepicker; import android.widget.edittext; import android.widget.imageview; import android.widget.linearlayout.layoutparams; import android.widget.popupwindow; import android.widget.textview; public class addictiondetails extends activity { textview tv_addiction_name, tv_today, tv_yesterday, tv_this_week, tv_this_month, tv_total; imageview iv_back, iv_filter; int year, month, day; int placedata; boolean state = false; datepickerdialog.ondatesetlistener datelistener; sqlitedatabase database; int cat_id = 0; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_addiction_details); intent intent = getintent(); final string cat_name = intent.getstringextra("cat_name"); tv_addiction_name = (textview) findviewbyid(r.id.textviewaddictiondetails); tv_addiction_name.settext(cat_name); tv_total = (textview) findviewbyid(r.id.textview_total); tv_today = (textview) findviewbyid(r.id.textview_today); tv_yesterday = (textview) findviewbyid(r.id.textview_yesterday); tv_this_week = (textview) findviewbyid(r.id.textview_this_week); tv_this_month = (textview) findviewbyid(r.id.textview_this_month); iv_back = (imageview) findviewbyid(r.id.imageviewaddictiondetailsback); iv_back.setclickable(true); iv_back.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { intent intent = new intent(addictiondetails.this, startactivity.class); startactivity(intent); } } ); iv_filter = (imageview) findviewbyid(r.id.imageviewaddictiondetailsfilter); iv_filter.setclickable(true); iv_filter.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { if(state == false) { state = true; layoutinflater layoutinflater = (layoutinflater)getbasecontext().getsystemservice(layout_inflater_service); final view popupview = layoutinflater.inflate(r.layout.activity_filter_addiction_form, null); final popupwindow popupwindow = new popupwindow(popupview, layoutparams.wrap_content, layoutparams.wrap_content); popupwindow.setwidth(300); popupwindow.setheight(300); final calendar cal = calendar.getinstance(); year = cal.get(calendar.year); month = cal.get(calendar.month); day = cal.get(calendar.day_of_month); final edittext etstartdate = (edittext)popupview.findviewbyid(r.id.edittextfilterstartdate); etstartdate.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { showdialog(1); placedata = 1; } } ); final edittext etenddate = (edittext)popupview.findviewbyid(r.id.edittextfilterenddate); etenddate.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { showdialog(1); placedata = 2; } } ); button btn_done = (button) popupview.findviewbyid(r.id.buttondone); btn_done.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { if(etstartdate.gettext()+""=="" || etenddate.gettext()+""=="") { alertdialog.builder adb = new builder(addictiondetails.this); adb.settitle("error"); adb.setmessage("fields can't blank."); adb.seticon(r.drawable.ic_launcher); adb.setpositivebutton("ok",new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { dialog.dismiss(); } } ); alertdialog advertisement = adb.create(); ad.show(); } else { simpledateformat sdf = new simpledateformat("dd/mm/yyyy"); date date1 = null; seek { date1 = sdf.parse(etstartdate.gettext()+""); } grab (parseexception e) { e.printstacktrace(); } date date2 = null; seek { date2 = sdf.parse(etenddate.gettext()+""); } grab (parseexception e) { e.printstacktrace(); } system.out.println(sdf.format(date1)); system.out.println(sdf.format(date2)); if(date1.compareto(date2)>0) { alertdialog.builder adb = new builder(addictiondetails.this); adb.settitle("error"); adb.setmessage("start date cannot greater end date."); adb.seticon(r.drawable.ic_launcher); adb.setpositivebutton("ok",new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { dialog.dismiss(); } } ); alertdialog advertisement = adb.create(); ad.show(); } else { intent intent = new intent(addictiondetails.this, filtereddata.class); intent.putextra("start_date", etstartdate.gettext()+""); intent.putextra("end_date", etenddate.gettext()+""); intent.putextra("cat_id", cat_id); intent.putextra("cat_name", cat_name); startactivity(intent); } } } } ); button btn_cancel = (button) popupview.findviewbyid(r.id.buttoncancel); btn_cancel.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { popupwindow.dismiss(); state = false; } } ); datelistener = new datepickerdialog.ondatesetlistener() { @override public void ondateset(datepicker view, int yr, int monthofyear, int dayofmonth) { simpledateformat tmpdateformat = new simpledateformat("dd/mm/yyyy"); calendar tmpc1 = calendar.getinstance(); tmpc1.set(yr, monthofyear, dayofmonth); date tmpd1 = tmpc1.gettime(); string tmpdatestring = tmpdateformat.format(tmpd1); update(placedata, tmpdatestring); } private void update(int position, string strdate) { switch(position) { case 1: log.e("in addiction details in startdate", strdate); etstartdate.settext(strdate); break; case 2: log.e("in addiction details in enddate", strdate); etenddate.settext(strdate); break; } } }; popupwindow.showasdropdown(iv_filter, 0, 0); } } } ); loaddb(); cursor cursor = database.query("category", new string[]{"cat_id"}, "cat_name=?", new string[]{cat_name}, null, null, null); //----------------------fetching id------------------- if(cursor.getcount() > 0) while(cursor.movetonext()) cat_id = cursor.getint(0); cursor.close(); //----------------------fetching total------------------- int totalcounter; cursor totalcursor = database.rawquery("select sum(cat_attribute_unit) counter cat_id ="+cat_id+";", null); if(totalcursor.movetonext()) totalcounter = totalcursor.getint(0); else totalcounter = 0; totalcursor.close(); tv_total.settext(totalcounter+""); //----------------------fetching today's count--------------- calendar cal1 = calendar.getinstance(); simpledateformat dateformat1 = new simpledateformat("dd/mm/yyyy"); string todays_date = dateformat1.format(cal1.gettime()); int todaycounter; cursor todaycursor = database.rawquery("select sum(cat_attribute_unit) counter cat_id ="+cat_id+" , counter_entry_date = '"+todays_date+"';", null); if(todaycursor.movetonext()) todaycounter = todaycursor.getint(0); else todaycounter = 0; todaycursor.close(); tv_today.settext(todaycounter+""); //----------------------fetching yesterdays count------------------ calendar cal2 = calendar.getinstance(); simpledateformat dateformat2 = new simpledateformat("dd/mm/yyyy"); cal2.add(calendar.date, -1); string yesterdays_date = dateformat2.format(cal2.gettime()); int yesterdaycounter = 0; cursor yesterdaycursor = database.rawquery("select sum(cat_attribute_unit) counter cat_id ="+cat_id+" , counter_entry_date = '"+yesterdays_date+"';", null); if(yesterdaycursor.movetofirst()) yesterdaycounter = yesterdaycursor.getint(0); else yesterdaycounter = 0; yesterdaycursor.close(); tv_yesterday.settext(yesterdaycounter+""); //-------------------------fetching current week count-------------------- simpledateformat weekdateformat = new simpledateformat("dd/mm/yyyy"); calendar c1 = calendar.getinstance(); c1.set(calendar.day_of_week, 1); date weekfirstdate = c1.gettime(); string strweekfirstday = weekdateformat.format(weekfirstdate); cursor weekcursor = database.rawquery("select sum(cat_attribute_unit) counter cat_id ="+cat_id+" , counter_entry_date between '"+strweekfirstday+"' , '"+todays_date+"';", null); int weekcounter = 0; if(weekcursor.movetofirst()) weekcounter = weekcursor.getint(0); else weekcounter = 0; weekcursor.close(); tv_this_week.settext(weekcounter+""); //----------------------fetching current month count-------------------- simpledateformat monthdateformat = new simpledateformat("dd/mm/yyyy"); calendar c2 = calendar.getinstance(); c2.set(calendar.day_of_month, 1); date monthfirstdate = c2.gettime(); string strmonthfirstday = monthdateformat.format(monthfirstdate); cursor monthcursor = database.rawquery("select sum(cat_attribute_unit) counter cat_id ="+cat_id+" , counter_entry_date between '"+strmonthfirstday+"' , '"+todays_date+"';", null); int monthcounter = 0; if(monthcursor.movetofirst()) monthcounter = monthcursor.getint(0); else monthcounter = 0; monthcursor.close(); tv_this_month.settext(monthcounter+""); database.close(); } private void loaddb() { database = openorcreatedatabase("addictioncounter.db", sqlitedatabase.open_readwrite, null); } protected dialog oncreatedialog(int id) { homecoming new datepickerdialog(this, datelistener, year, month, day); } } also not able refer class within if loop of onclicklistener. gives error while giving ctrl+space.
destroy popupview at
popupwindow.setondismisslistener android android-view
No comments:
Post a Comment