title: Threads inside Fragment
body: <p>I have an app using a Fragment with a View pager. I query the android contacts database (this should be fast) to get primary numbers of every contact to show in a ListView. The problem is, this seems to take some time. I tried to use a Thread and an ASyncTask inside onCreateView and "update" or set my adapter for my contact list. When I do this I'm getting some errors:</p>
  
  <blockquote>
    <p>can't create handler inside thread that has not called Looper.prepare()</p>
  </blockquote>
  
  <p>I tried using Looper.prepare() but it would still crash.</p>
  
  <p>So I put in a handler, but there is a lag when people launch my app</p>
  
  <pre><code>final Runnable setupView = new Runnable() {
          @Override
          public void run() {
              contactos  = con.getContactSearch("");
              setupContacts();
          }
      };
  </code></pre>
  
  <p>where:
  getContactSearch:</p>
  
  <pre><code>public HashMap&lt;String, ContactInfo&gt; getContactSearch(String _where)
       {
           contactInfo = new HashMap&lt;String, ContactInfo&gt;();
           Cursor cursor = null ;
           if(_where.length()==0)
           {
              cursor = getContacts();
           }else
           {
               cursor = getContactsFilter(_where);
           }
           try
           {
           while (cursor.moveToNext()) {
                String id = cursor.getString(cursor
                        .getColumnIndex(ContactsContract.Contacts._ID));
                if(!contactInfo.containsKey(id))
                {
  
                    String displayName = cursor.getString(cursor
                            .getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
                  //obtenemos el telefono principal
                   //String number = "" ;//getPrimaryNumber(id);
                    ContactInfo ci = new ContactInfo(displayName,"",id);
                    contactInfo.put(id,ci);
                }
              }
              getPrimaryNumbers();
  
  
           }finally
           {
               cursor.close();
           }
           return contactInfo;
       }
  </code></pre>
  
  <p>and setupcontacts :</p>
  
  <pre><code>    mAdapter = new DialerContactsAdapter(contactos,getActivity());
          listaContactos.setAdapter(mAdapter);
          listaContactos.setFastScrollEnabled(true);
          listaContactos.setOnScrollListener(new OnScrollListener() {
              @Override
              public void onScrollStateChanged(AbsListView view, int scrollState) {
                  switch (scrollState) {
                  case OnScrollListener.SCROLL_STATE_IDLE:
                  mAdapter.mBusy = false;
                  mAdapter.notifyDataSetChanged();
                  break;
                  case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
                      mAdapter.mBusy = false;
                      mAdapter.notifyDataSetChanged();
                  break;
                  case OnScrollListener.SCROLL_STATE_FLING:
                      mAdapter.mBusy = true;
                  break;
                  }
              }
  
              @Override
              public void onScroll(AbsListView view, int firstVisibleItem,
                      int visibleItemCount, int totalItemCount) {
                  // TODO Auto-generated method stub
  
              }
          });
  </code></pre>
  
  <p>How I do this asynchronously? I'm wondering if I do only the searching of contacts in onCreate() what will happen, if its faster than onCreateView, the ListView will be null. Doing this "works" but there is a great lag.
  How I can optimize this process?</p>