android - Using Loader to display data in an AlertDialog? -
i looking way display data retrieved loader sqlite db in alertdialog
. clicking name in listview
transfer db entry id dialogfragment
, , based on this, additional data should loaded. loading such work seems happen after alertdialog
displayed because message box shows null value. know if possible @ transfer data alertdialog
this? help!
import java.text.simpledateformat; import java.util.calendar; import java.util.date; import android.annotation.suppresslint; import android.app.alertdialog; import android.app.dialog; import android.content.contenturis; import android.content.dialoginterface; import android.database.cursor; import android.os.bundle; import android.os.handler; import android.support.v4.app.dialogfragment; import android.support.v4.app.loadermanager.loadercallbacks; import android.support.v4.content.cursorloader; import android.support.v4.content.loader; @suppresslint("simpledateformat") public class birthdaydetailsmessage extends dialogfragment implements loadercallbacks<cursor> { static string date_format; static final string calendar = "calendar"; private long mrowid; private string mname; private string mdate; private boolean myearknown; private string mage; private calendar mcalendar; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); mcalendar = calendar.getinstance(); date_format = prefdateformat.getprefdateformat(getactivity()); bundle arguments = getarguments(); mrowid = arguments.getlong(reminderprovider.column_rowid); getloadermanager().initloader(0, null, this); } @override public dialog oncreatedialog(bundle savedinstancestate){ alertdialog.builder builder = new alertdialog.builder(getactivity()); builder.setmessage("birthday: "+ mdate +"\n\n"+"age: "+ mage) .settitle(mname) .setcancelable(false) .setpositivebutton("ok", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { dialog.cancel(); } }); return builder.create(); } @override public loader<cursor> oncreateloader(int id, bundle args) { return new cursorloader(getactivity(), contenturis.withappendedid( reminderprovider.content_uri, mrowid), null, null, null, null); } @override public void onloadfinished(loader<cursor> loader, cursor reminder) { if (reminder.getcount() == 0) { new handler().post(new runnable() { @override public void run() { ((onfinisheditor) getactivity()).finisheditor(); } }); return; } mname = reminder.getstring(reminder .getcolumnindexorthrow(reminderprovider.column_name)); myearknown = (reminder.getint(reminder .getcolumnindexorthrow(reminderprovider.column_year))==0? false:true); long dateinmillis = reminder.getlong(reminder .getcolumnindexorthrow(reminderprovider.column_date)); date date = new date(dateinmillis); mcalendar.settime(date); simpledateformat dateformat = new simpledateformat(date_format); mdate = dateformat.format(mcalendar.gettime()); if (myearknown == true){ mage = string.valueof(getdiffyears.getdiffyears(mcalendar)); }else{ mage = "age not known"; } } @override public void onloaderreset(loader<cursor> arg0) { // nothing reset fragment } }
edit:
i removed oncreatedialog method , used following view instead
public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.dialog_fragment, container, false); getdialog().settitle("name"); date = (textview) v.findviewbyid(r.id.date); age = (textview) v.findviewbyid(r.id.age); getloadermanager().initloader(0, null, this); return v; } private void updatevalues() { getdialog().settitle(mname); date.settext("geburtstag: "+ mdate); age.settext("alter: " + mage); }
in included updatevalues() in onloadfinished, , created layout xml message body...this way seems work properly.
what want use call method loader
create dialog. not put loader
in dialog class, either in calling class or separate class. call/create dialog in onloadfinished
. implementation similar asynctask
.
alternately, create dialog default text, , retain reference in calling class. in onloadfinished
update values of dialog. i'm pretty sure 1 work, haven't tested it.
Comments
Post a Comment