title: How to get the user's choice of File, back from a DialogFragment list
body: <p>I have done  extensive reading of a variety of answers on S/O in the search for a solution to my problem, but now seem to be going round and round in circles as there's doesn't appear to be any consistent approach to DialogFragment (DF)  creation and handling which I've been able to use to solve my problem. </p>
  
  <p>I am running a single activity with many ordinary fragments. I have created  a DF  to handle a list of files from which I want the user to pick one, which will then be imported (into the SQLite database file).
  I am creating the DF  from my MainActivity by the following code (instigated by a button click in an ordinary fragment) :-</p>
  
  <pre><code>public void importdb(View view) {
  
  
  
      NewFileChooser newFileChooser = new NewFileChooser();
      newFileChooser.setTargetFragment(newFileChooser, FILE_CHOOSER);
      FragmentTransaction ft = getFragmentManager().beginTransaction();
      ft.addToBackStack(null);
      newFileChooser.show(ft, "Diag");
  }
  </code></pre>
  
  <p>Within 'NewFileChooser' the DF is created  within the OnCreateView method :- </p>
  
  <pre><code>@Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  
      View view = inflater.inflate(R.layout.new_layout, container, false);
      list = (ListView)view.findViewById(R.id.file_list);
      Context c = getActivity();
      currentDir = new File("/sdcard/PH100-Backup");
      fill(currentDir);
      Dialog mydialog = getDialog();
      mydialog.setTitle("Choose a database File to Import");
  
      //list.setOnClickListener(???????????); // position to set listener ??
  
      return view;
   }
  </code></pre>
  
  <p>The  'fill(currentDir)'  method  shown below uses 2 additional classes, employing an ArrayAdapter  means of getting the files from the sdcard /backup directory and displaying this sorted list in the  list area  of the  DF.<br>
  This part works fine, the  DF is created and displays the list, and if you click any of the files on the list, the background lightens,  indicating you're starting the event, but that’s as far as I can get.   </p>
  
  <pre><code>private void fill(File f) {
      File[] dirs = f.listFiles();
      getDialog().setTitle("Current Dir: " + f.getName());
      List&lt;Option&gt; dir = new ArrayList&lt;Option&gt;();
      List&lt;Option&gt; fls = new ArrayList&lt;Option&gt;();
      try {
          for (File ff : dirs) {
              if (ff.isDirectory())
              dir.add(new Option(ff.getName(), "Folder", ff.getAbsolutePath()));
              else {
                  fls.add(new Option(ff.getName(), "File Size: " + ff.length(), ff.getAbsolutePath()));
              }
          }
      } catch (Exception e) {
  
      }
      Collections.sort(dir);
      Collections.sort(fls);
      dir.addAll(fls);
      if (!f.getName().equalsIgnoreCase("sdcard"))
      adapter = new FileArrayAdapter(getActivity(), R.layout.file_view, dir);
      list.setAdapter(adapter);
  }
  </code></pre>
  
  <p>The code is largely extracted from the 'ticked' answer  <a href="https://stackoverflow.com/questions/19665123/file-chooser-inside-dialogfragment-re-using-code">File Chooser inside DialogFragment (re-using code)</a> </p>
  
  <p>I have amended this to suit my circumstances and it works, but only as far as I mentioned above.
  From reading many of the existing answers on S/O I'm not sure whether I've dug myself into a hole. Many of the answers have buttons employed with listeners associated with them, which I do not have and so haven't been able to use their code (particularly on listeners) to solve my problem,  others  start the DF off using the<br>
   -- AlertDialog.Builder  builder = new AlertDialog.Builder(getActivity());  -- 
  code construct  which I'm not using. (I did try to use this method but the .setItems verb didn't like my dynamically built file list, it just wanted a fixed array, and I couldn't get it to work) .</p>
  
  <p>Clearly I need to use a listener interface as a means to get the file name back to the MainActivity but I don't know how to get this working.
  I've tried using the NewFileChooser implementing  AdapterView.OnItemSelectedListener   which forced me to implement the method below </p>
  
  <pre><code>@Override
  public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
  
  }
  </code></pre>
  
  <p>But I couldn't get this to work either. I'm using Android studio and even with the code completion helper I seem to be pretty stuck. </p>
  
  <p>I'm afraid this looks like a case of …when you know you're in a hole stop digging…!  So any assistance would be very gratefully received  </p>