title: Converting android file browser ListActivity example into a Fragment
body: <p>I am trying to figure out a way to use this example in a fragment:
  <a href="http://sampleprogramz.com/android/browse.php" rel="nofollow noreferrer">http://sampleprogramz.com/android/browse.php</a></p>
  
  <p>I have changed the OnCreate method to the OnCreateView method and have tried to correct any of the errors that Android Studio flags but I can't seem to get it working.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>import java.io.File;
  import java.io.FilenameFilter;
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.Comparator;
  import java.util.List;
  import android.app.ListActivity;
  import android.content.Context;
  import android.content.Intent;
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.ListFragment;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ArrayAdapter;
  import android.widget.ImageView;
  import android.widget.ListView;
  import android.widget.TextView;
  
  import static android.app.Activity.RESULT_OK;
  
  public class FilePicker extends ListFragment {
  
      public final static String EXTRA_FILE_PATH = "file_path";
      public final static String EXTRA_SHOW_HIDDEN_FILES = "show_hidden_files";
      public final static String EXTRA_ACCEPTED_FILE_EXTENSIONS = "accepted_file_extensions";
      private final static String DEFAULT_INITIAL_DIRECTORY = "/";
  
      protected File Directory;
      protected ArrayList&lt;File&gt; Files;
      protected FilePickerListAdapter Adapter;
      protected boolean ShowHiddenFiles = false;
      protected String[] acceptedFileExtensions;
  
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  
          View emptyView = inflater.inflate(R.layout.empty_view, container, false);
          ((ViewGroup) getListView().getParent()).addView(emptyView);
          getListView().setEmptyView(emptyView);
  
          // Set initial directory
          Directory = new File(DEFAULT_INITIAL_DIRECTORY);
  
          // Initialize the ArrayList
          Files = new ArrayList&lt;File&gt;();
  
          // Set the ListAdapter
          Adapter = new FilePickerListAdapter(getActivity(), Files);
          setListAdapter(Adapter);
  
          // Initialize the extensions array to allow any file extensions
          acceptedFileExtensions = new String[] {".rc"};
  
          // Get intent extras
          if(getActivity().getIntent().hasExtra(EXTRA_FILE_PATH))
              Directory = new File(getActivity().getIntent().getStringExtra(EXTRA_FILE_PATH));
  
          if(getActivity().getIntent().hasExtra(EXTRA_SHOW_HIDDEN_FILES))
              ShowHiddenFiles = getActivity().getIntent().getBooleanExtra(EXTRA_SHOW_HIDDEN_FILES, false);
  
          if(getActivity().getIntent().hasExtra(EXTRA_ACCEPTED_FILE_EXTENSIONS)) {
  
              ArrayList&lt;String&gt; collection =
                      getActivity().getIntent().getStringArrayListExtra(EXTRA_ACCEPTED_FILE_EXTENSIONS);
  
              acceptedFileExtensions = (String[])
                      collection.toArray(new String[collection.size()]);
          }
          return emptyView;
      }
  
      @Override
      public void onResume() {
          refreshFilesList();
          super.onResume();
      }
  
      protected void refreshFilesList() {
  
          Files.clear();
          ExtensionFilenameFilter filter =
                  new ExtensionFilenameFilter(acceptedFileExtensions);
  
          File[] files = Directory.listFiles(filter);
  
          if(files != null &amp;&amp; files.length &gt; 0) {
  
              for(File f : files) {
  
                  if(f.isHidden() &amp;&amp; !ShowHiddenFiles) {
  
                      continue;
                  }
  
                  Files.add(f);
              }
  
              Collections.sort(Files, new FileComparator());
          }
  
          Adapter.notifyDataSetChanged();
      }
  
  
  
      public void onListItemClick(ListView l, View v, int position, long id) {
  
          File newFile = (File)l.getItemAtPosition(position);
  
          if(newFile.isFile()) {
  
              Intent extra = new Intent();
              extra.putExtra(EXTRA_FILE_PATH, newFile.getAbsolutePath());
              getActivity().setResult(RESULT_OK, extra);
              getActivity().finish();
          }
          else {
  
              Directory = newFile;
              refreshFilesList();
          }
  
          super.onListItemClick(l, v, position, id);
      }
  
      private class FilePickerListAdapter extends ArrayAdapter&lt;File&gt; {
  
          private List&lt;File&gt; mObjects;
  
          public FilePickerListAdapter(Context context, List&lt;File&gt; objects) {
  
              super(context, R.layout.list_item, android.R.id.text1, objects);
              mObjects = objects;
          }
  
          @Override
          public View getView(int position, View convertView, ViewGroup parent) {
  
              View row = null;
  
              if(convertView == null) {
  
                  LayoutInflater inflater = (LayoutInflater)
                          getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  
                  row = inflater.inflate(R.layout.list_item, parent, false);
              }
              else
                  row = convertView;
  
              File object = mObjects.get(position);
  
              ImageView imageView = (ImageView)row.findViewById(R.id.file_picker_image);
              TextView textView = (TextView)row.findViewById(R.id.file_picker_text);
              textView.setSingleLine(true);
              textView.setText(object.getName());
  
              if(object.isFile())
                  imageView.setImageResource(R.drawable.sound);
  
              else
                  imageView.setImageResource(R.drawable.folder);
  
              return row;
          }
      }
  
      private class FileComparator implements Comparator&lt;File&gt; {
  
          public int compare(File f1, File f2) {
  
              if(f1 == f2)
                  return 0;
  
              if(f1.isDirectory() &amp;&amp; f2.isFile())
                  // Show directories above files
                  return -1;
  
              if(f1.isFile() &amp;&amp; f2.isDirectory())
                  // Show files below directories
                  return 1;
  
              // Sort the directories alphabetically
              return f1.getName().compareToIgnoreCase(f2.getName());
          }
      }
  
      private class ExtensionFilenameFilter implements FilenameFilter {
  
          private String[] Extensions;
  
          public ExtensionFilenameFilter(String[] extensions) {
  
              super();
              Extensions = extensions;
          }
  
          public boolean accept(File dir, String filename) {
  
              if(new File(dir, filename).isDirectory()) {
  
                  // Accept all directory names
                  return true;
              }
  
              if(Extensions != null &amp;&amp; Extensions.length &gt; 0) {
  
                  for(int i = 0; i &lt; Extensions.length; i++) {
  
                      if(filename.endsWith(Extensions[i])) {
  
                          // The filename ends with the extension
                          return true;
                      }
                  }
                  // The filename did not match any of the extensions
                  return false;
              }
              // No extensions has been set. Accept all file extensions.
              return true;
          }
      }
  }
  </code></pre>
  
  <p>Thanks.</p>