i have been searching hours no luck. using eclipse create android app timepicker , datepicker, datepicker works fine when call on timepicker listener in oncreatedialog comes timepickerlistener cannot resolved variable error.
i have followed whole heap of examples me looks i'm replicating can't rid of error. in particular resolution @ bottom of question
using datepicker , timepicker dialog boxes - android java in eclipse
my java code follows:
private textview tvdisplaydate; private datepicker dpresult; private timepicker timepicker; private button infringementbutton; private button timebutton; private textview tvdisplaydate2; private textview tvdisplaytime; private int year; private int month; private int day; private int yeara; private int montha; private int daya; private int hour; private int minute; static final int date_dialog_id = 1; static final int time_dialog_id = 2; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); setcurrentdateonview(); setcurrenttimeonview(); addlisteneronbutton(); } // display current date public void setcurrentdateonview() { tvdisplaydate = (textview) findviewbyid(r.id.tvdate); tvdisplaydate2 = (textview) findviewbyid(r.id.tvdatea); dpresult = (datepicker) findviewbyid(r.id.dpresult); final calendar c = calendar.getinstance(); year = c.get(calendar.year); month = c.get(calendar.month); day = c.get(calendar.day_of_month); hour = c.get(calendar.hour_of_day); minute = c.get(calendar.minute); // set current date textview tvdisplaydate.settext(new stringbuilder() // month 0 based, add 1 .append(day).append("-").append(month + 1).append("-") .append(year).append(" ")); c.add(calendar.day_of_month, 42); daya = c.get(calendar.day_of_month); montha = c.get(calendar.month); yeara = c.get(calendar.year); tvdisplaydate2.settext(new stringbuilder() // month 0 based, add 1 .append(daya).append("-").append(montha + 1).append("-") .append(yeara).append(" ")); // set current date datepicker dpresult.init(year, month, day, null); } //display current time public void setcurrenttimeonview() { tvdisplaytime = (textview) findviewbyid(r.id.tvtime); timepicker = (timepicker) findviewbyid(r.id.timepicker); // set current time textview tvdisplaytime.settext( new stringbuilder().append(pad(hour)) .append(":").append(pad(minute))); // set current time timepicker timepicker.setcurrenthour(hour); timepicker.setcurrentminute(minute); } public void addlisteneronbutton() { infringementbutton = (button) findviewbyid(r.id.infringementbutton); timebutton = (button) findviewbyid(r.id.timebutton); infringementbutton.setonclicklistener(new onclicklistener() { @suppresswarnings("deprecation") @override public void onclick(view view) { showdialog(date_dialog_id); } }); timebutton.setonclicklistener(new view.onclicklistener() { @suppresswarnings("deprecation") @override public void onclick(view v) { showdialog(time_dialog_id); } }); } protected dialog oncreatedialog(int id) { switch (id) { case date_dialog_id: // set date picker current date return new datepickerdialog(this, datepickerlistener, year, month,day); break; switch (id) { case time_dialog_id: // set time picker current time return new timepickerdialog(this, timepickerlistener, hour, minute,false);} return null; }} private datepickerdialog.ondatesetlistener datepickerlistener = new datepickerdialog.ondatesetlistener() { // when dialog box closed, below method called. public void ondateset(datepicker view, int selectedyear, int selectedmonth, int selectedday) { year = selectedyear; month = selectedmonth; day = selectedday; // set selected date textview tvdisplaydate.settext(new stringbuilder() .append(day).append("-").append(month + 1).append("-").append(year) .append(" ")); calendar c = new gregoriancalendar(); c.set(calendar.year,year); c.set(calendar.month, month); c.set(calendar.day_of_month, day); c.add(calendar.day_of_month,42); daya = c.get(calendar.day_of_month); montha = c.get(calendar.month); yeara = c.get(calendar.year); tvdisplaydate2.settext(new stringbuilder() .append(daya).append("-").append(montha + 1).append("-").append(yeara) .append(" ")); // set selected date datepicker dpresult.init(year, month, day, null); } private timepickerdialog.ontimesetlistener timepickerlistener = new timepickerdialog.ontimesetlistener() { public void ontimeset(timepicker view, int selectedhour, int selectedminute) { hour = selectedhour; minute = selectedminute; // set current time textview tvdisplaytime.settext(new stringbuilder().append(pad(hour)) .append(":").append(pad(minute))); // set current time timepicker timepicker.setcurrenthour(hour); timepicker.setcurrentminute(minute); } }; }; private static string pad(int c) { if (c >= 10) return string.valueof(c); else return "0" + string.valueof(c); }
here's main.xml
<scrollview xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/scrollview01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"> <tablelayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="@dimen/margin" android:orientation="vertical" tools:context=".mainactivity" > <tablerow> <button android:id="@+id/infringementbutton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/infringement" /> <datepicker android:id="@+id/dpresult" android:layout_width="fill_parent" android:layout_height="wrap_content" android:visibility="gone" /> <textview android:id="@+id/tvdate" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" /> </tablerow><tablerow> <button android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/penaltydate" /> <textview android:id="@+id/tvdatea" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" /> </tablerow> <tablerow > <button android:id="@+id/timebutton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/time" /> <timepicker android:id="@+id/timepicker" android:layout_width="fill_parent" android:layout_height="wrap_content" android:visibility="gone" /> <textview android:id="@+id/tvtime" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" android:textappearance="?android:attr/textappearancelarge" /> </tablerow>
hopefully code isn't confusing cos i'm new @ stuff. thanks.
i have commented layout related code dnt have nd u have not post .... once check ths out , let me knw ...
public class dialogscreen extends activity { private textview tvdisplaydate; private datepicker dpresult; private timepicker timepicker; private button infringementbutton; private button timebutton; private textview tvdisplaydate2; private textview tvdisplaytime;
private int year; private int month; private int day; private int yeara; private int montha; private int daya; private int hour; private int minute; static final int date_dialog_id = 1; static final int time_dialog_id = 2; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); setcurrentdateonview(); setcurrenttimeonview(); addlisteneronbutton(); } // display current date public void setcurrentdateonview() { tvdisplaydate = (textview) findviewbyid(r.id.tvdate); tvdisplaydate2 = (textview) findviewbyid(r.id.tvdatea); dpresult = (datepicker) findviewbyid(r.id.dpresult); final calendar c = calendar.getinstance(); year = c.get(calendar.year); month = c.get(calendar.month); day = c.get(calendar.day_of_month); hour = c.get(calendar.hour_of_day); minute = c.get(calendar.minute); // set current date textview tvdisplaydate.settext(new stringbuilder() // month 0 based, add 1 .append(day).append("-").append(month + 1).append("-") .append(year).append(" ")); c.add(calendar.day_of_month, 42); daya = c.get(calendar.day_of_month); montha = c.get(calendar.month); yeara = c.get(calendar.year); tvdisplaydate2.settext(new stringbuilder() // month 0 based, add 1 .append(daya).append("-").append(montha + 1).append("-") .append(yeara).append(" ")); // set current date datepicker dpresult.init(year, month, day, null); } // display current time public void setcurrenttimeonview() { tvdisplaytime = (textview) findviewbyid(r.id.tvtime); timepicker = (timepicker) findviewbyid(r.id.timepicker); // set current time textview tvdisplaytime.settext(new stringbuilder().append(pad(hour)).append(":") .append(pad(minute))); // set current time timepicker timepicker.setcurrenthour(hour); timepicker.setcurrentminute(minute); } public void addlisteneronbutton() { infringementbutton = (button) findviewbyid(r.id.infringementbutton); timebutton = (button) findviewbyid(r.id.timebutton); infringementbutton.setonclicklistener(new view.onclicklistener() { @suppresswarnings("deprecation") @override public void onclick(view view) { showdialog(date_dialog_id); } }); timebutton.setonclicklistener(new view.onclicklistener() { @suppresswarnings("deprecation") @override public void onclick(view v) { showdialog(time_dialog_id); } }); } protected dialog oncreatedialog(int id) { switch (id) { case date_dialog_id: // set date picker current date return new datepickerdialog(this, datepickerlistener, year, month, day); case time_dialog_id: // set time picker current time return new timepickerdialog(this, timepickerlistener, hour, minute, false); } return null; } private datepickerdialog.ondatesetlistener datepickerlistener = new datepickerdialog.ondatesetlistener() { // when dialog box closed, below method called. public void ondateset(datepicker view, int selectedyear, int selectedmonth, int selectedday) { year = selectedyear; month = selectedmonth; day = selectedday; // set selected date textview tvdisplaydate.settext(new stringbuilder().append(day).append("-") .append(month + 1).append("-").append(year).append(" ")); calendar c = new gregoriancalendar(); c.set(calendar.year, year); c.set(calendar.month, month); c.set(calendar.day_of_month, day); c.add(calendar.day_of_month, 42); daya = c.get(calendar.day_of_month); montha = c.get(calendar.month); yeara = c.get(calendar.year); tvdisplaydate2.settext(new stringbuilder().append(daya).append("-") .append(montha + 1).append("-").append(yeara).append(" ")); // set selected date datepicker dpresult.init(year, month, day, null); } }; private timepickerdialog.ontimesetlistener timepickerlistener = new timepickerdialog.ontimesetlistener() { public void ontimeset(timepicker view, int selectedhour, int selectedminute) { hour = selectedhour; minute = selectedminute; // set current time textview tvdisplaytime.settext(new stringbuilder().append(pad(hour)) .append(":").append(pad(minute))); // set current time timepicker timepicker.setcurrenthour(hour); timepicker.setcurrentminute(minute); } }; private static string pad(int c) { if (c >= 10) return string.valueof(c); else return "0" + string.valueof(c); }
}
xml resource
<?xml version="1.0" encoding="utf-8"?>
<tablelayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <tablerow> <button android:id="@+id/infringementbutton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="infringement" /> <datepicker android:id="@+id/dpresult" android:layout_width="fill_parent" android:layout_height="wrap_content" android:visibility="gone" /> <textview android:id="@+id/tvdate" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" /> </tablerow> <tablerow> <button android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="penaltydate" /> <textview android:id="@+id/tvdatea" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" /> </tablerow> <tablerow> <button android:id="@+id/timebutton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="time" /> <timepicker android:id="@+id/timepicker" android:layout_width="fill_parent" android:layout_height="wrap_content" android:visibility="gone" /> <textview android:id="@+id/tvtime" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" android:textappearance="?android:attr/textappearancelarge" /> </tablerow> </tablelayout>
Comments
Post a Comment