dialog - Android: Using data from TimePickerDialog in PreferenceActivity -


i trying create setting sets time. want time used in fragment. don't know (and can't find) how data timepickerdialog , use in fragment/activity. know need use sharedpreferences confused (especially after reading this: http://developer.android.com/guide/topics/ui/settings.html !!).

i followed timepicker in preferencescreen create dialog

the time stored long easier use calendar. dialog works fine - time set , restored properly.

code:

settings.java

public class settings extends preferenceactivity implements onsharedpreferencechangelistener {       @override         public void oncreate(bundle savedinstancestate) {             super.oncreate(savedinstancestate);              // load preferences xml resource             addpreferencesfromresource(r.xml.preferences);          }      @override     public void onsharedpreferencechanged(sharedpreferences arg0, string arg1) {         // todo auto-generated method stub      } 

this custom dialog allows placed in preference screen

public class tpp2 extends dialogpreference {          private calendar calendar;         private timepicker picker = null;           public tpp2(context ctxt) {             this(ctxt, null);         }          public tpp2(context ctxt, attributeset attrs) {             super(ctxt, attrs);              setpositivebuttontext("set");             setnegativebuttontext("cancel");             calendar = new gregoriancalendar();         }          @override         protected view oncreatedialogview() {             picker = new timepicker(getcontext());              return (picker);         }          @override         protected void onbinddialogview(view v) {             super.onbinddialogview(v);              picker.setcurrenthour(calendar.get(calendar.hour_of_day));             picker.setcurrentminute(calendar.get(calendar.minute));          }          @override         protected void ondialogclosed(boolean positiveresult) {             super.ondialogclosed(positiveresult);              if (positiveresult) {                 calendar.set(calendar.hour_of_day, picker.getcurrenthour());                 calendar.set(calendar.minute, picker.getcurrentminute());                  persistlong (calendar.gettimeinmillis()); //                  setsummary(getsummary());                 if (callchangelistener(calendar.gettimeinmillis())) {                     persistlong(calendar.gettimeinmillis());                     notifychanged();                 }             }         }          @override         protected object ongetdefaultvalue(typedarray a, int index) {             return (a.getstring(index));         }          @override         protected void onsetinitialvalue(boolean restorevalue, object defaultvalue) {              if (restorevalue) {                 if (defaultvalue == null) {                     calendar.settimeinmillis(getpersistedlong(system                             .currenttimemillis()));                  } else {                     calendar.settimeinmillis(long                             .parselong(getpersistedstring((string) defaultvalue)));                  }             } else {                 if (defaultvalue == null) {                     calendar.settimeinmillis(system.currenttimemillis());                  } else {                     calendar.settimeinmillis(long.parselong((string) defaultvalue));                  }             }             setsummary(getsummary());         }          @override         public charsequence getsummary() {             if (calendar == null) {                 picker.setcurrenthour(8);                 picker.setcurrentminute(0);                 return null;             }             return dateformat.gettimeformat(getcontext()).format(                     new date(calendar.gettimeinmillis()));         }     } 

code preferences:

 <preferencecategory android:title="title" >         <com.xxx.tpp2             android:key="rdtime"             android:summary="summary"             android:title="set time" />     </preferencecategory> 

thank you


Comments

Popular posts from this blog

php - Passing multiple values in a url using checkbox -

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -