title: Return ArrayList from async task
body: <p>1) I already read all questions about that here, so please tell me more, than just url from SO...</p>
  
  <p>2) I want to return only ONE variable from asynctask, but it can be message error or user details. So I decided to return ArrayList with indexes 0 (for error messages) and 1 (for success).</p>
  
  <p><strong>AsyncTask</strong></p>
  
  <pre><code>public class LoginGet extends AsyncTask&lt;String, ProgressBar, ArrayList&lt;String&gt;&gt; {
  ...
  public ArrayList&lt;String&gt; result = new ArrayList&lt;String&gt;();
  
  public LoginGet(Context mContext, String loginText, String passwordText) {
      //Get values form LoginActivity
  }
  
  protected void onPreExecute(){
      super.onPreExecute();
      // progressDialog
  }
  
  @Override
  protected ArrayList doInBackground(String... arg0){
      try {
          // Making HTTP Request
          try {
              // Creating HTTP client
              ...
              ...
              JSONObject jObject = new JSONObject(json);
              success = jObject.getString("success");
              result.add(0, "");
              result.add(1, "");
              if (success != null) {
                  if (success == "1") {
                      result.add(1, jObject.getString("hash"));
                      return result;
                  } else if (success == "0") {
                      result.add(0, jObject.getString("error"));
                      return result;
                  }
              } else {
                  result.add(0, "Error while logging.");
                  return result;
              }
  
             // writing response to log
              Log.d("Http Response:", response.toString());
          }  catch (UnsupportedEncodingException e) {
              e.printStackTrace();
          } catch (ClientProtocolException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
  
  
      }catch (Exception e) {
  
      }
  
      return result;
  }
  
  @Override
  protected void onPostExecute(ArrayList&lt;String&gt; result) {
      ((LoginActivity)context).getResult(result);
      progressDialog.dismiss();
  }
  }
  </code></pre>
  
  <p><strong>LoginActivity</strong></p>
  
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      ...
  
      loginButton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              ...
              if (loginText.compareTo("") == 0 || passwordText.compareTo("") == 0) {
                  ...
              }else{
                  LoginGet loginPost = new LoginGet(LoginActivity.this,loginText,passwordText);
                  loginPost.execute();
              }
          }
      });
  
  }
  
  // Get result from asynctask LoginGet
  public void getResult(ArrayList&lt;String&gt; result){
      if (result.get(0) != "") {
          loginButton.setEnabled(true);
          Toast.makeText(LoginActivity.this, result.get(0), Toast.LENGTH_SHORT).show();
      } else if (result.get(1) != "") {
          SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(LoginActivity.this);
          SharedPreferences.Editor editor = preferences.edit();
          editor.putString("Login", result.get(1));
          editor.commit();
  
          Intent intent = new Intent(LoginActivity.this, MainActivity.class);
          startActivity(intent);
      }
  
  }
  </code></pre>
  
  <p><strong>Error:</strong></p>
  
  <pre><code>java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
  at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
  at java.util.ArrayList.get(ArrayList.java:308)
  at cz.jacon.smsapp.LoginActivity.getResult(LoginActivity.java:57)
  at cz.jacon.smsapp.background.LoginGet.onPostExecute(LoginGet.java:121)
  at cz.jacon.smsapp.background.LoginGet.onPostExecute(LoginGet.java:30)
  </code></pre>
  
  <p>3) In my AsyncTask I have <code>result.add(0, "");</code> and <code>result.add(1, "");</code>, so why is the ArrayList size 0?</p>