Code example for AlertDialog.Builder

Methods: setAdapter, setTitle, show

0
 } 
 
 @Override 
 protected Dialog onCreateDialog(int id) {
  Dialog dialog = null;
  AlertDialog.Builder builder = new Builder(this);
 
  if (fileList == null) { 
   Log.e(TAG, "No files loaded");
   dialog = builder.create();
   return dialog;
  } 
 
  switch (id) {
  case DIALOG_LOAD_FILE: 
   builder.setTitle("Browse your file");
   builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
    @Override 
    public void onClick(DialogInterface dialog, int which) {
     chosenFile = fileList[which].file;
     File sel = new File(path + "/" + chosenFile);
     if (sel.isDirectory()) {
      firstLvl = false; 
 
      // Adds chosen directory to list 
      str.add(chosenFile); 
      fileList = null; 
      path = new File(sel + "");
 
      loadFileList(); 
      // refresh the list being shown 
      removeDialog(DIALOG_LOAD_FILE); 
      showDialog(DIALOG_LOAD_FILE); 
 
      Log.d(TAG, path.getAbsolutePath());
     } 
 
     // Checks if 'up' was clicked 
     else if (chosenFile.equalsIgnoreCase("up") && !sel.exists()) {
 
      // present directory removed from list 
      String s = str.remove(str.size() - 1);
 
      // path modified to exclude present directory 
      path = new File(path.toString().substring(0,
        path.toString().lastIndexOf(s)));
      fileList = null; 
 
      // if there are no more directories in the list, then 
      // its the first level 
      if (str.isEmpty()) { 
       firstLvl = true; 
      } 
      loadFileList(); 
 
      removeDialog(DIALOG_LOAD_FILE); 
      showDialog(DIALOG_LOAD_FILE); 
      Log.d(TAG, path.getAbsolutePath());
 
     } 
     // File picked.......supposed to return fileName chosen by 
     // the User 
     else { 
      // Perform action with file picked 
      Intent intent = getIntent();
      String returnVal = path.getPath() + "/" + chosenFile;
      intent.putExtra("fileChosen", returnVal);
      setResult(RESULT_OK, intent);
      // for purpose of testing 
      // Toast.makeText(getBaseContext(), 
      // returnVal,Toast.LENGTH_LONG).show(); 
      finish(); 
     } 
    } 
   }); 
   break; 
  } 
 
  dialog = builder.show();
  return dialog;
 } 
}