2014-11-05 59 views
0

我有一个DialogFragment它提供了一个选项,使用startActivityForResult选择一个文件。然后我使用onActivityResult来接收数据和请求代码。在使用filemanager选择文件后,我从onActivityResult访问它。现在选择的实际文件并不多,因为主要问题是当文件进入时我可以得到它的名字和其他东西。现在我将其添加到HashMap<String, Object>,然后将其添加到ArrayList<HashMap<String, Object>>。然后,我想使用带有自定义xml布局的SimpleAdapter来填充列表视图。问题是SimpleAdapter需要上下文作为参数。我怎样才能在onActivityResult()中接收上下文?如何在onActivityResult中获取上下文?

一些代码来获取的我在做什么更好的画面:

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全码:

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(); 
     } 


    } 
} 

最大的问题我看到这里是startActivityOnResult完成后,我们回到我们的活动到对话框,变量设置为n呃,我点击初始化的按钮。

新的更新

@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(); 
    } 

而且当startActivtiyForResult被称为onPause()onStop()onDestroyView()永远不会被调用。

它仍然不起作用。

最后更新

我想道歉,为愚蠢的,我是做高度。在我的托管Activity这是MainActivity.java,当这种Activity会打电话onActivityResult()我会创造对话片段的新实例为这样:new MyDialogFragment().onActivityResult(),显然这就是为什么没有你的家伙方法工作作为onCreateView不叫这个时候。我已将new MyDialogFragment()更改为先前初始化的对话框片断,我实际上正在显示并且现在一切正常。我将结束这个问题。

+1

您需要活动的上下文吗? 'Context Context' lol ..然后在你的'onResume()'实例化'context = this'现在调用它'SimpleAdapter simpleAdapter = new SimpleAdapter(context,attachments_list,R.layout.mycustomlayout,keys,ids);'你还好吗? – Elltz 2014-11-05 04:05:36

+0

这不起作用 – someguy234 2014-11-05 04:07:12

+0

尝试getActivity() – Endor 2014-11-05 04:07:43

回答

1

UPDATE

如果DialogFragment不会添加到后退堆栈,那么你可以尝试使用 setRetainInstance (boolean retain)当你第一次创建它。

原来的答案

我想你需要修改你的程序流程一点点。

简而言之,您应该在onActivityCreated中设置您的ListView,ArrayList为数据,并设置Adapter

这样,在用户可以从DialogFragment中选择一个文件之前,您可以让您的ListView及其适配器准备好接收新数据。

然后在onActivityResult块中,将数据添加到ArrayList并在您的Adapter上调用notifyDatasetChanged

+0

我也在想这个,我会试着告诉 – someguy234 2014-11-05 04:22:14

+0

我试试,但是我得到了一个我在UPATE下粘贴的错误。 – someguy234 2014-11-05 04:54:01

+0

你能发布你的片段的完整代码吗? – Endor 2014-11-05 05:34:33

0

ohkay试试这个,先生..假设你的活动课是Gurinderhans;

static Gurinderhans ellt; // lol 

然后仍然在你的活动类。

static Gurinderhans getGurinderhans(){ 
    return ellt;   
} 

然后在活动的onCreate()

Gurinderhans.ellt = this; //or ellt = this; 
在dialogfragment

然后onActivityResult做到这一点

Gurinderhans.getGurinderhans() // this is my class, activity and context.. 

所以应该像这样

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
... 
//here I want to get the context 
SimpleAdapter simpleAdapter = new SimpleAdapter(Gurinderhans.getGurinderhans(), attachments_list, R.layout.mycustomlayout, keys, ids); 
... 
} 

让我如果它有助于..

+0

对不起,但你的方法不起作用 – someguy234 2014-11-05 07:04:48

+0

先生?兄弟?它应该工作..我把这个评论,因为你的问题没有指定它==请这样做..请删除你的代码,调用startactivity和所有从oncreatView到onActivitycreated方法......我肯定会有不是一个空指针异常..这是一个疯狂的猜测,你也会得到同样的错误,当你使用这个? @gurinderhans – Elltz 2014-11-05 08:53:44

+0

我会尽快尝试,让你知道 – someguy234 2014-11-05 17:06:11