title: setOnItemClickListener on ListView after AsyncTask finished
body: <p>I've setup a <code>ListView</code> and a <code>SimpleAdapter</code> that get data through a function which runs in a <code>AsyncTask</code> but the problem is that I now want to set an <code>setOnItemClickListener</code>on this <code>ListView</code>.</p>
  
  <p>The problem is that I'm trying to attach a <code>setOnItemClickListener</code> but this is getting attached before my <code>AsyncTask</code> is finished in the <code>onLoad</code>, which means there is no bind.</p>
  
  <p>So my question is how can I let the <code>Async</code> execute and finish to fill up my <code>ListView</code> and bind the <code>setOnItemClickListener</code> <strong>after</strong> my <code>Async</code> task is finished?</p>
  
  <p><strong>Note:</strong> I've updated my answer with the full code of the class and the view, since this was asked by an user and will give more insight to others. For the full code please scroll down to the <strong>Update</strong> part!</p>
  
  <p>The code:</p>
  
  <pre><code>    @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.contact_overview);
      //Context inladen
      context = ContactOverview.this;
  
          listViewContacts = (ListView)findViewById(R.id.listViewContacts);
  
      //new GetAllContactsAsync().execute();
      asny.execute();
              Log.i("finished?","finished?");
              Log.i("listLength", "" + listViewContacts.getCount());
              listViewContacts.setOnItemClickListener(new OnItemClickListener() {
                  public void onItemClick(AdapterView parent, View view, int position, long id) {
                      Toast.makeText(ContactOverview.this, "ee", Toast.LENGTH_SHORT).show();
                      Log.i("test","test");
                  }
  
              }); 
  }
  </code></pre>
  
  <p>the <code>listViewContacts.setOnItemClickListener</code> should be attached <strong>after</strong> the <code>Async</code> task is finished. So how should I do this? If my <code>listViewContacts.setOnItemClickListener</code>is triggered before my <code>Async</code> task is done the bind will not be made..</p>
  
  <p><strong>UPDATE: FULL CODE</strong></p>
  
  <p><strong>ListView (contact_overview.xml)</strong></p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="8dp"
  android:paddingLeft="8dp"
  android:paddingRight="8dp"
  android:paddingTop="8dp"
  tools:context=".MainActivity" 
  android:background="#1e78bc"&gt;
  
  &lt;ListView
      android:id="@+id/listViewContacts"
      android:background="@android:color/white"
      android:shadowColor="#f9f9f9"
      android:shadowDx="1"
      android:shadowDy="1"
      android:shadowRadius="1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" &gt;
  &lt;/ListView&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><strong>Row for listview (row.xml)</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"&gt;
  
  &lt;TextView 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/customerName"
  android:paddingLeft="3dp"
  android:textSize="18sp"
  android:drawableLeft="@drawable/user"
  android:gravity="center_vertical"
  android:textStyle="bold"/&gt;
  
  &lt;TextView android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/customerPhone"
  android:paddingTop="3dp"
  android:paddingRight="3dp"
  android:gravity="center_vertical"
  android:layout_below="@id/customerName"
  android:drawableLeft="@drawable/telefoon"
  android:autoLink="phone"/&gt;
  
  &lt;TextView android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/customerGsm"
  android:paddingTop="3dp"
  android:paddingRight="3dp"
  android:layout_below="@id/customerName"
  android:gravity="center_vertical|right"
  android:layout_toRightOf="@id/customerPhone"
  android:drawableLeft="@drawable/telefoon"
  android:autoLink="phone"/&gt;
  
  &lt;TextView android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:id="@+id/customerEmail"
  android:drawableLeft="@drawable/email"
  android:layout_below="@id/customerPhone"
  android:gravity="center_vertical"
  android:autoLink="email"/&gt;
  
  &lt;TextView 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/customerZip"
  android:paddingLeft="3dp"
  android:drawableLeft="@drawable/user"
  android:layout_below="@id/customerEmail"/&gt;
  
  &lt;TextView 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/customerCity"
  android:paddingLeft="3dp"
  android:layout_toRightOf="@id/customerZip"
  android:layout_below="@+id/customerEmail"/&gt;
  
  &lt;TextView android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:id="@+id/customerStreet"
  android:paddingRight="3dp"
  android:layout_toRightOf="@id/customerCity"
  android:layout_below="@+id/customerEmail"/&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><strong>Class for filling the ListView (ContactOverview.java)</strong></p>
  
  <pre><code>public class ContactOverview extends ActionBarActivity  {
  public int uid = 0;
  final String url = "dev.myurl.com";
  final int port = 8072;
  final String dbName = "dbname";
  final String username = "dbPass";
  private ProgressDialog progressDialog;
  Context context;
  ArrayList&lt;Object&gt; customerArray = new ArrayList&lt;Object&gt;();
  ArrayList&lt;Object&gt; companyArray = new ArrayList&lt;Object&gt;();
  List companyList = null;
  List customerList = null;
  ListView listViewContacts;
  int finished = 0;
  GetAllContactsAsync asny = new GetAllContactsAsync();
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.contact_overview);
      //Context inladen
      context = ContactOverview.this;
      //new GetAllContactsAsync().execute();
      listViewContacts =  (ListView) findViewById(R.id.listViewContacts);
      asny.execute();
      listViewContacts.setOnItemClickListener(new OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView parent, View view, int position, long id) {
              Toast.makeText(ContactOverview.this, "Clicked", Toast.LENGTH_SHORT).show();
          }
  
      });
  }
  
  private class GetAllContactsAsync extends AsyncTask&lt;Void, Integer, Void&gt; 
  {
      //Before running code in separate thread  
      @Override  
      protected void onPreExecute()  
      {  
          //Create a new progress dialog  
          progressDialog = new ProgressDialog(ContactOverview.this);  
          //Set the progress dialog to display a horizontal progress bar  
          progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
          //Set the dialog title to 'Loading...'  
          progressDialog.setTitle(R.string.loading);  
          //Set the dialog message to 'Loading application View, please wait...'  
          progressDialog.setMessage(ContactOverview.this.getString(R.string.loadingMessage));  
          //This dialog can't be canceled by pressing the back key  
          progressDialog.setCancelable(false);  
          //This dialog isn't indeterminate  
          progressDialog.setIndeterminate(false);  
          //The maximum number of items is 100  
          progressDialog.setMax(100);  
          //Set the current progress to zero  
          progressDialog.setProgress(0);
          //Display the progress dialog  
          progressDialog.show();  
      }  
  
  protected Void doInBackground(Void... params)  
      {  
          try  
          {  
              //Get the current thread's token  
              synchronized (this)  
              {  
                  //counter progress dialog  
                  int counter = 0;  
                  //Wait 100 milliseconds  
                  this.wait(200);
                  try 
                  {
                      //get uid
                      uid = Connect(url, port, dbName, username, "myPass");
                  } 
                  catch (MalformedURLException e1) 
                  {
                      //Toast.makeText(MainActivity.this, context.getString(R.string.authenticateError), Toast.LENGTH_LONG).show();
                      e1.printStackTrace();
                  }
  
                  //Models en connectie maken voor xml-rpc
                  XmlRpcClient models = null;
                  try 
                  {
                      models = new XmlRpcClient() {{
                          /*was: 
                          setConfig(new XmlRpcClientConfigImpl() {{
                          setServerURL(new URL(String.format("http://%s:%s/xmlrpc/2/object", "dev.myurl.com", port)));
                          }});
                          }};
                          */
                      setConfig(new XmlRpcClientConfigImpl() {
                          private static final long serialVersionUID = 1L;
  
                      {
                      setServerURL(new URL(String.format("http://%s:%s/xmlrpc/2/object", "dev.myurl.com", port)));
                      }});
                      }};
                  } 
                  catch (MalformedURLException e1) 
                  {
                      e1.printStackTrace();
                  }
                  //Loading to 20%
                  counter += 20;
                  publishProgress(counter);
                  try
                  {
                      /*final Array list = (Array)models.execute("execute_kw", Arrays.asList(
                      dbName, uid, "myPass",
                      "res.partner", "search",
                      Arrays.asList(new HashMap() {{
                          Arrays.asList("is_company", "=", "true");
                          Arrays.asList("customer", "=", "true");
                          }})
                              ));
                      Log.i("Array", "" + list);*/
  
  
                      customerList = (List)Arrays.asList((Object[])models.execute("execute_kw", Arrays.asList(
                      dbName, uid, "myPass",
                      "res.partner", "search_read",
                      Arrays.asList(Arrays.asList(
                          Arrays.asList("customer", "=", true))),
                      new HashMap() {
                          private static final long serialVersionUID = 1L;
  
                      {
                          put("fields", Arrays.asList("name", "phone", "mobile","email","website", "street", "city", "zip"));
                          put("limit", 0);
                      }}
                      )));
  
                      Log.i("customerList",""+customerList);
  
                      //30% further in progressdialog
                      counter += 30;
                      publishProgress(counter);
  
                      companyList = (List)Arrays.asList((Object[])models.execute("execute_kw", Arrays.asList(
                      dbName, uid, "myPass",
                      "res.partner", "search_read",
                      Arrays.asList(Arrays.asList(
                          Arrays.asList("is_company", "=", true))),
                      new HashMap() {
                          private static final long serialVersionUID = 1L;
                      {
                          put("fields", Arrays.asList("name", "phone", "mobile","email","website", "street", "city", "zip"));
                          put("limit", 0);
                      }}
                      )));
  
                      //Another 30% further (= 80%)
                      counter += 30;
                      publishProgress(counter);
                      //customerArray = new ArrayList&lt;Object&gt;(Arrays.asList(customerObject));
                      //companyArray = new ArrayList&lt;Object&gt;(Arrays.asList(companyObject));
                      //Log.i("Customers", "object: " + customerObject);
  
                      //parsing, now at @90%.
                      counter += 20;
                      publishProgress(counter);
                      //100% after data is parsed in parseDataInLijsten()
                      counter += 10;
                      publishProgress(counter);
                  } 
                  catch (XmlRpcException e) 
                  {
                      e.printStackTrace();
                  }
  
                  publishProgress(counter);
              }  
          }
          catch (InterruptedException e)  
          {  
              e.printStackTrace();  
          }  
          return null;  
      }  
  
  
   //Update the progress  
      @Override  
      protected void onProgressUpdate(Integer... values)  
      {  
          //set the current progress of the progress dialog  
          progressDialog.setProgress(values[0]);  
      }  
  
      //Parse data from here on and then show it. (parseDataInLijsten does this)  
      @Override  
      protected void onPostExecute(Void result)  
      {  
          //close the progress dialog  
          progressDialog.dismiss();  
          //initialize the View    
          //show ListView on screen. 
          parseDataInLijsten();
             Log.i("listViewContacts987", "" + listViewContacts.getCount());
      }  
  }
  
  //Finally parse the data and show it in the ListView.
  private void parseDataInLijsten()
  {
  
      //Vars
      HashMap testMap = null;
      String phone = null;
      String gsm = null;
      String id = null;
      String email = null;
      String name = null;
      String street = null;
      String city = null;
      String website = null;
      String zip = null;
      List&lt;String&gt; listItems=new ArrayList&lt;String&gt;();
      //ArrayList&lt;String&gt; listItems=new ArrayList&lt;String&gt;();
  
      listViewContacts = (ListView)findViewById(R.id.listViewContacts);
  
      ArrayList&lt;HashMap&lt;String, String&gt;&gt; mylist = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
  
      ArrayList&lt;HashMap&lt;String, String&gt;&gt; mylist2 = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
      HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
  
      //Loopen over every record in the list. This is per company.
      for(int teller = 0; teller &lt; companyList.size(); teller++)
      {
          //HashMap contains one set 
          testMap = (HashMap) companyList.get(teller);
          int teller2 = 0;
          //every value is an item in the list of a record
          for (Object value : testMap.values()) {
              teller2++;
              if(teller2 == 1)
              {
                  street = value.toString();
                  map = new HashMap&lt;String, String&gt;();
                  map.put("street", street);
              }
              if(teller2 == 2)
              {
                  id = value.toString();
                  map.put("id",id);
              }
              if(teller2 == 3)
              {
                  gsm = value.toString();
                  if(gsm == "false")
                  {
                      gsm = "-";
                  }
                  map.put("gsm", gsm);
              }
              if(teller2 == 4)
              {
                  website = value.toString();
                  if(website == "false")
                  {
                      website = "-";
                  }
                  map.put("website", website);
              }
              if(teller2 == 5)
              {
                  city = value.toString();
                  if(city == "false")
                  {
                      city = "";
                  }
                  map.put("city", city + " - ");
              }
              if(teller2 == 6)
              {
                  email = value.toString();
                  if(email == "false")
                  {
                      email = "";
                  }
                  map.put("email", email);
              }
              if(teller2 == 7)
              {
                  phone = value.toString();
                  if(phone == "false")
                  {
                      phone = "-";
                  }
                  map.put("phone", phone);
              }
              if(teller2 == 8)
              {
                  name = value.toString();
                  map.put("name", name);
              }
              if(teller2 == 9)
              {
                  zip = value.toString();
                  map.put("zip", zip + " ");
                  if(zip == "false")
                  {
                      zip = "";
                  }
                  mylist2.add(map);
              }
          }
  
      }
  
  
      SimpleAdapter mSchedule = new SimpleAdapter(this, mylist2, R.layout.row,
                  new String[] {"name","phone", "gsm", "email", "zip", "city", "street"}, new int[] {R.id.customerName, R.id.customerPhone, R.id.customerGsm,
              R.id.customerEmail,
              R.id.customerZip, R.id.customerCity, R.id.customerStreet});
      listViewContacts.setAdapter(mSchedule);
  }
  }
  </code></pre>
  
  <p>Thanks
  Yenthe</p>