I have a DialogFragment
which gives an option to pick a file using startActivityForResult
. I then use onActivityResult
to recieve the data and request code. After the file is chosen using the filemanager I access it from onActivityResult. Not doing much with the actual file being chosen right now as the main problem is when the file comes in I can get its name, other stuff. Now I add this into a HashMap<String, Object>
which is then added to ArrayList<HashMap<String, Object>>
. I then want to use a SimpleAdapter
with a custom xml layout to populate the listview. The problem is SimpleAdapter requires the context as a parameter. How can I recieve the context in onActivityResult()?
Some Code to get a better picture of what I am doing:
public class MyFragment extends DialogFragment {
...
...
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
...
//here I want to get the context
SimpleAdapter simpleAdapter = new SimpleAdapter(thecontext, attachments_list, R.layout.mycustomlayout, keys, ids);
...
}
}
UPDATE:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SimpleAdapter.notifyDataSetChanged()' on a null object reference
UPDATE FULL CODE:
public class MyFragment extends DialogFragment {
//code for whole activity
public static final String NEW_NOTE_CARD_FRAGMENT_CODE = "1";
//codes for 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 the an xml layout to a linearlayout which is the layout of this 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 for attachment files
attachmentsListView = (ListView) newnoteattachment_window.findViewById(R.id.attachmentsListView);
String attachmentName = "My test file ";
String attachmentDateAdded = "Added: Nov ";
//create random data
for (int i = 0; i < 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 for this 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));
}
});
return 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 ";
for (int i = 0; i < 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 I am seeing here is after the startActivityOnResult finishes and we come back into our activity to the dialog, the variables are set to null, the ones I 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 the startActivtiyForResult
is called the onPause()
and onStop()
but onDestroyView()
is never called.
It still doesn't work.
FINAL UPDATE
I would like to apologize to for the height of stupidity that I was making. In my hosting Activity
which is MainActivity.java
, when this Activity
would call onActivityResult()
I would create a new instance of the dialog fragment as such: new MyDialogFragment().onActivityResult()
and obviously this is why none of your guys methods worked as onCreateView
wasn't called this time. I have change new MyDialogFragment()
to the previously initialized dialog fragment that I am actually displaying and everything works now. And I will close this question.