android - Reading data selected in TimePicker called from a Fragment -
i have fragment
has own layout , has choose time button along textview
beneath it. opening timepickerfragment
fragment
when user clicks button. when user selects time, , clicks done, want populate textview
contained in fragment. not sure how this.
all materials/tutorials online invoke timepicker
activity. (obviously) have parent activity, since timepicker being used in fragment, limit scope as possible.
flow follows:
parentactivity
> fragment (contains textview , button)
> dialogfragment (timepicker)
here implementation:
public class timepickerfragment extends dialogfragment implements timepickerdialog.ontimesetlistener { @override public dialog oncreatedialog(bundle savedinstancestate) { int hour = 0; int minute = 0; boolean is24hour = false; // ... code read shared preference, correct time format, theme, etc. return new timepickerdialog(getactivity(), dialog_theme, this, hour, minute, is24hour); //dateformat.is24hourformat(getactivity()) } @override public void ontimeset(timepicker view, int hour, int minute) { //textview textview = (textview) getview.findviewbyid(r.id.time_value); //nullpointerexception! textview textview = (textview) view.findviewbyid(r.id.time_value); //also nullpointerexception! // time chosen user textview.settext(timeanddate.getdisplayabletime(time)); textview.clearfocus(); } }
it belongs fragment invokes dialogfragment
findviewbyid
looks view id in current view hierarchy. textview
not child of timerview
.
so need initialize textview
in fragment not dialogfragment
.
all fragment-to-fragment communication done through associated activity. 2 fragments should never communicate directly. use interface call activity , communciate time fragment.
http://developer.android.com/training/basics/fragments/communicating.html
dialogfragment --> parentactivtiy --> fragment
you can check example @
Comments
Post a Comment