title: How to return an Array list from AsyncTask into Main Activity?
body: <p>I'm trying to display the list of files in a dropbox account in my app. My question is, how do I return the list (fnames) into my MainActivity so I can display it in a listview.
  And how do I call it in the MainActivity too?
  New to android development, any other tips will be appreciated!</p>
  
  <p>Thanks!</p>
  
  <pre><code>public class DbFileExplorer extends AsyncTask&lt;Void, Long, Boolean&gt; {
  
      private Context mContext;
      private final ProgressDialog mDialog;
      private DropboxAPI&lt;?&gt; mApi;
      private String mPath;
  
      private FileOutputStream mFos;
  
      private boolean mCanceled;
      private Long mFileLen;
      private String mErrorMsg;
      protected String[] fnames;
  
      public DbFileExplorer(Context context, DropboxAPI&lt;?&gt; api,
              String dropboxPath, String[] efnames){
          // We set the context this way so we don't accidentally leak activities
          mContext = context.getApplicationContext();
          fnames = efnames;
          mApi = api;
          mPath = dropboxPath;
  
          mDialog = new ProgressDialog(context);
          mDialog.setMessage("Opening Directory");
          mDialog.show();
      }
  
      @Override
      protected Boolean doInBackground(Void... params){
          // Get the metadata for a directory
          int i = 0;
          try{
              fnames = null;
              Entry dirent = mApi.metadata(mPath, 1000, null, true, null);
              ArrayList&lt;Entry&gt; files = new ArrayList&lt;Entry&gt;();
              ArrayList&lt;String&gt; dir = new ArrayList&lt;String&gt;();
  
              for (Entry ent: dirent.contents){
                  //Add it to the list of thumbs we can choose from
                  files.add(ent);
                  dir.add(new String(files.get(i++).path));
              }
              i=0;
              fnames = dir.toArray(new String[dir.size()]);
  
  
              return true;
          } catch (DropboxUnlinkedException e) {
              // The AuthSession wasn't properly authenticated or user unlinked.
          } catch (DropboxPartialFileException e) {
              // We canceled the operation
              mErrorMsg = "Download canceled";
          } catch (DropboxServerException e) {
              // Server-side exception.  These are examples of what could happen,
              // but we don't do anything special with them here.
              if (e.error == DropboxServerException._304_NOT_MODIFIED) {
                  // won't happen since we don't pass in revision with metadata
              } else if (e.error == DropboxServerException._401_UNAUTHORIZED) {
                  // Unauthorized, so we should unlink them.  You may want to
                  // automatically log the user out in this case.
              } else if (e.error == DropboxServerException._403_FORBIDDEN) {
                  // Not allowed to access this
              } else if (e.error == DropboxServerException._404_NOT_FOUND) {
                  // path not found (or if it was the thumbnail, can't be
                  // thumbnailed)
              } else if (e.error == DropboxServerException._406_NOT_ACCEPTABLE) {
                  // too many entries to return
              } else if (e.error == DropboxServerException._415_UNSUPPORTED_MEDIA) {
                  // can't be thumbnailed
              } else if (e.error == DropboxServerException._507_INSUFFICIENT_STORAGE) {
                  // user is over quota
              } else {
                  // Something else
              }
              // This gets the Dropbox error, translated into the user's language
              mErrorMsg = e.body.userError;
              if (mErrorMsg == null) {
                  mErrorMsg = e.body.error;
              }
          } catch (DropboxIOException e) {
              // Happens all the time, probably want to retry automatically.
              mErrorMsg = "Network error.  Try again.";
          } catch (DropboxParseException e) {
              // Probably due to Dropbox server restarting, should retry
              mErrorMsg = "Dropbox error.  Try again.";
          } catch (DropboxException e) {
              // Unknown error
              mErrorMsg = "Unknown error.  Try again.";
          }
          return false;   
      }
  
      @Override
      protected void onProgressUpdate(Long... progress){
          int percent = (int)(100.0*(double)progress[0]/mFileLen + 0.5);
          mDialog.setProgress(percent);
          }
  
      @Override
      protected void onPostExecute(Boolean result){
          mDialog.dismiss();
      }
  
  
      private void showToast(String msg){
          Toast error = Toast.makeText(mContext, msg, Toast.LENGTH_LONG);
          error.show();
      }
  }
  </code></pre>