java - How to get context inside onActivityResult? -
i have dialogfragment
gives alternative pick file using startactivityforresult
. utilize onactivityresult
recieve info , request code. after file chosen using filemanager access onactivityresult. not doing much actual file beingness chosen right main problem when file comes in can name, other stuff. add together hashmap<string, object>
added arraylist<hashmap<string, object>>
. want utilize simpleadapter
custom xml layout populate listview. problem simpleadapter requires context parameter. how can recieve context in onactivityresult()?
some code improve image of doing:
public class myfragment extends dialogfragment { ... ... @override public void onactivityresult(int requestcode, int resultcode, intent data) { ... //here want context simpleadapter simpleadapter = new simpleadapter(thecontext, attachments_list, r.layout.mycustomlayout, keys, ids); ... } }
update:
java.lang.nullpointerexception: effort invoke virtual method 'void android.widget.simpleadapter.notifydatasetchanged()' on null object reference
update total code:
public class myfragment extends dialogfragment { //code whole activity public static final string new_note_card_fragment_code = "1"; //codes each menu button public static final string pick_file_req_code = "2"; public static final string pick_new_image_code = "3"; //attachment keys public static final string key_attachment_name = "a_name"; public static final string key_attachment_date_added = "a_date_added"; public static final string key_attachment_icon = "a_icon"; arraylist<hashmap<string, object>> attachmentslistarray = new arraylist<hashmap<string, object>>(); //listview listview attachmentslistview; simpleadapter simpleadapter; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.newnotecard, container, false); //dialog customizations getdialog().getwindow().requestfeature(window.feature_no_title); // set color transparent getdialog().getwindow().setbackgrounddrawable(new colordrawable(color.transparent)); final imagebutton addnewattachment = (imagebutton) v.findviewbyid(r.id.addnewattachment); //addnewattachment addnewattachment.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //this adds xml layout linearlayout layout of dialog //new attachment window layoutinflater thismenulayoutinflater = (layoutinflater) getactivity().getapplicationcontext().getsystemservice(context.layout_inflater_service); view newnoteattachment_window = thismenulayoutinflater.inflate(r.layout.newnotenewattachment, newnotewindowextras, false); newnotewindowextras.addview(newnoteattachment_window); //listview attachment files attachmentslistview = (listview) newnoteattachment_window.findviewbyid(r.id.attachmentslistview); string attachmentname = "my test file "; string attachmentdateadded = "added: nov "; //create random info (int = 0; < 3; i++) { hashmap<string, object> singleattachment = new hashmap<string, object>(); singleattachment.put(key_attachment_name, attachmentname + i); singleattachment.put(key_attachment_date_added, attachmentdateadded + (i + 1)); attachmentslistarray.add(singleattachment); } string[] keys = {key_attachment_name, key_attachment_date_added}; int[] ids = {r.id.attachment_name, r.id.attachment_date_added}; simpleadapter = new simpleadapter(getactivity().getapplicationcontext(), attachmentslistarray, r.layout.individualattachmentlayout, keys, ids); attachmentslistview.setadapter(simpleadapter); //the actual action button intent openfileexplorerintent = new intent(intent.action_get_content); openfileexplorerintent.settype("*/*"); getactivity().startactivityforresult(openfileexplorerintent, integer.parseint(new_note_card_fragment_code + pick_file_req_code)); } }); homecoming v; } @override public void onactivityresult(int requestcode, int resultcode, intent data) { int reqcode_l = character.getnumericvalue(integer.tostring(requestcode).charat(1)); if (reqcode_l == integer.parseint(pick_file_req_code)) {//do file pick stuff //new individual file window log.i("menu action", "file pick: " + data.getdata()); string attachmentname = "title "; string attachmentdateadded = "edited: nov "; (int = 0; < 6; i++) { hashmap<string, object> singleattachment = new hashmap<string, object>(); singleattachment.put(key_attachment_name, attachmentname + (i+1)); singleattachment.put(key_attachment_date_added, attachmentdateadded + (i + 1)); //singleattachment.put(key_attachment_icon, tmp_attachmenticon); attachmentslistarray.add(singleattachment); } simpleadapter.notifydatasetchanged(); } } }
the big problem seeing here after startactivityonresult finishes , come our activity dialog, variables set null, ones initialize onbutton click.
new update
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setretaininstance(true); log.i("lifecycle", "oncreate"); } @override public void ondestroyview() { log.i("lifecycle", "fragment ondestroyview"); if (getdialog() != null && getretaininstance()) getdialog().setdismissmessage(null); super.ondestroyview(); }
also when startactivtiyforresult
called onpause()
, onstop()
ondestroyview()
never called.
it still doesn't work.
final update
i apologize height of stupidity making. in hosting activity
mainactivity.java
, when activity
phone call onactivityresult()
create new instance of dialog fragment such: new mydialogfragment().onactivityresult()
, why none of guys methods worked oncreateview
wasn't called time. have alter new mydialogfragment()
initialized dialog fragment displaying , works now. , close question.
update
if dialogfragment not added stack, can seek using setretaininstance (boolean retain) when first create it.
original answer
i think need modify programme flow little.
simply put, should set listview
, arraylist
data, , adapter
in onactivitycreated.
this way, before user can go pick file dialogfragment, have listview , adapter ready receive new data.
then in onactivityresult block, add together info arraylist , phone call notifydatasetchanged
on adapter.
java android listview android-context onactivityresult
No comments:
Post a Comment