title: Implementation of AsyncTask in a Fragment
body: <p>Right now I have three scrollable tab fragments each with a list of installed apps. However, when I hit the button to go to the activity and then scroll through the listview it is extremely slow. </p>
  
  <p><img src="https://i.stack.imgur.com/KWZcv.png" alt="enter image description here"></p>
  
  <p>I am trying to create an AsyncTask subclass in my fragments and prepare my adapter in doInBackground() and call setAdapter in onPostExecute().</p>
  
  <p>I have tried these:</p>
  
  <p><a href="https://stackoverflow.com/questions/18558084/implement-asynctask-in-fragment-android">implement AsyncTask in Fragment android</a></p>
  
  <p><a href="http://javatechig.com/android/how-to-get-list-of-installed-apps-in-android" rel="nofollow noreferrer">http://javatechig.com/android/how-to-get-list-of-installed-apps-in-android</a></p>
  
  <p>but not too much luck.</p>
  
  <p>This is my modified code:</p>
  
  <pre><code>package com.spicycurryman.getdisciplined10.app;
  
  import android.content.pm.ApplicationInfo;
  import android.content.pm.PackageInfo;
  import android.content.pm.PackageItemInfo;
  import android.content.pm.PackageManager;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.AdapterView;
  import android.widget.AdapterView.OnItemClickListener;
  import android.widget.ListView;
  
  import com.ibc.android.demo.appslist.app.ApkAdapter;
  
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.Comparator;
  import java.util.List;
  
  
  public class InstalledAppActivity extends Fragment
          implements OnItemClickListener {
  
      PackageManager packageManager;
      ListView apkList;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
  
          setHasOptionsMenu(true);
          View rootView = inflater.inflate(R.layout.user_installed, container, false);
          AppBoost appBoost = new AppBoost();
          appBoost.execute(apkList);
  
  
          return rootView;
  
      }
  
  
  
      private class AppBoost extends AsyncTask{
          ListView apkList;
  
          @Override
          protected Object doInBackground(Object[] objects) {
              packageManager = getActivity().getPackageManager();
              List&lt;PackageInfo&gt; packageList = packageManager
                      .getInstalledPackages(PackageManager.GET_PERMISSIONS);
  
              List&lt;PackageInfo&gt; packageList1 = new ArrayList&lt;PackageInfo&gt;();
  
          /*To filter out System apps*/
              for(PackageInfo pi : packageList) {
                  boolean b = isSystemPackage(pi);
                  if(!b) {
                      packageList1.add(pi);
                  }
              }
  
              //sort by application name
  
              final PackageItemInfo.DisplayNameComparator comparator = new PackageItemInfo.DisplayNameComparator(packageManager);
  
              Collections.sort(packageList1, new Comparator&lt;PackageInfo&gt;() {
                  @Override
                  public int compare(PackageInfo lhs, PackageInfo rhs) {
                      return comparator.compare(lhs.applicationInfo, rhs.applicationInfo);
                  }
  
  
              });
  
  
              apkList.setAdapter(new ApkAdapter(getActivity(), packageList1, packageManager));
  
              apkList.setOnItemClickListener((OnItemClickListener) this);
  
  
              return apkList;
          }
  
  
  
  
      }
  
      /**
       * Return whether the given PackgeInfo represents a system package or not.
       * User-installed packages (Market or otherwise) should not be denoted as
       * system packages.
       *
       * @param pkgInfo
       * @return boolean
       */
      private boolean isSystemPackage(PackageInfo pkgInfo) {
          return ((pkgInfo.applicationInfo.flags &amp; ApplicationInfo.FLAG_SYSTEM) != 0) ? true
                  : false;
      }
  
  
  // Don't need in Fragment
  /*@Override
  public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
      inflater.inflate(R.menu.block, menu);
     // super.onCreateOptionsMenu(menu,inflater);
  }*/
  
      @Override
      public void onItemClick(AdapterView&lt;?&gt; adapterView, View view, int i, long l) {
  
      }
  }
  </code></pre>
  
  <p>I'm not sure what I am doing wrong. Nothing shows up in the current tab, but it seems like I implemented everything properly. </p>
  
  <p><strong><em>Edit: Here is my adapter:</em></strong></p>
  
  <pre><code>package com.ibc.android.demo.appslist.app;
  
  import android.app.Activity;
  import android.content.pm.PackageInfo;
  import android.content.pm.PackageManager;
  import android.graphics.drawable.Drawable;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.BaseAdapter;
  import android.widget.CheckBox;
  import android.widget.TextView;
  import android.view.View.OnClickListener;
  
  import com.spicycurryman.getdisciplined10.app.R;
  
  import java.util.List;
  
  public class ApkAdapter extends BaseAdapter {
  
  
      List&lt;PackageInfo&gt; packageList;
      Activity context;
      PackageManager packageManager;
      boolean[] itemChecked;
  
      public ApkAdapter(Activity context, List&lt;PackageInfo&gt; packageList,
                        PackageManager packageManager) {
          super();
          this.context = context;
          this.packageList = packageList;
          this.packageManager = packageManager;
          itemChecked = new boolean[packageList.size()];
      }
  
      private class ViewHolder {
          TextView apkName;
          CheckBox ck1;
      }
  
      public int getCount() {
          return packageList.size();
      }
  
      public Object getItem(int position) {
          return packageList.get(position);
      }
  
      public long getItemId(int position) {
          return 0;
      }
  
      @Override
      public View getView(final int position, View convertView, ViewGroup parent) {
          final ViewHolder holder;
  
          LayoutInflater inflater = context.getLayoutInflater();
  
          if (convertView == null) {
              convertView = inflater.inflate(R.layout.installed_apps, null);
              holder = new ViewHolder();
  
              holder.apkName = (TextView) convertView
                      .findViewById(R.id.appname);
              holder.ck1 = (CheckBox) convertView
                      .findViewById(R.id.checkBox1);
  
              convertView.setTag(holder);
               //holder.ck1.setTag(packageList.get(position));
  
          } else {
  
              holder = (ViewHolder) convertView.getTag();
          }
          // ViewHolder holder = (ViewHolder) convertView.getTag();
          PackageInfo packageInfo = (PackageInfo) getItem(position);
  
          Drawable appIcon = packageManager
                  .getApplicationIcon(packageInfo.applicationInfo);
          String appName = packageManager.getApplicationLabel(
                  packageInfo.applicationInfo).toString();
          appIcon.setBounds(0, 0, 75, 75);
          holder.apkName.setCompoundDrawables(appIcon, null, null, null);
          holder.apkName.setCompoundDrawablePadding(15);
          holder.apkName.setText(appName);
          holder.ck1.setChecked(false);
  
          if (itemChecked[position])
              holder.ck1.setChecked(true);
          else
              holder.ck1.setChecked(false);
  
          holder.ck1.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
                  if (holder.ck1.isChecked())
                      itemChecked[position] = true;
                  else
                      itemChecked[position] = false;
              }
          });
  
          return convertView;
  
      }
  
  
  
  }
  </code></pre>