2014-11-05 62 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