0

i have this code and the scroll is slow

ApplicationAdapter

import java.util.List;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ApplicationAdapter extends ArrayAdapter<ApplicationInfo> {
    private List<ApplicationInfo> appsList = null;
    private Context context;
    private PackageManager packageManager;

    public ApplicationAdapter(Context context, int textViewResourceId,
            List<ApplicationInfo> appsList) {
        super(context, textViewResourceId, appsList);
        this.context = context;
        this.appsList = appsList;
        packageManager = context.getPackageManager();
    }

    @Override
    public int getCount() {
        return ((null != appsList) ? appsList.size() : 0);
    }

    @Override
    public ApplicationInfo getItem(int position) {
        return ((null != appsList) ? appsList.get(position) : null);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (null == view) {
            LayoutInflater layoutInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = layoutInflater.inflate(R.layout.snippet_list_row, null);
        }

        ApplicationInfo data = appsList.get(position);
        if (null != data) {
            TextView appName = (TextView) view.findViewById(R.id.app_name);
            TextView packageName = (TextView) view.findViewById(R.id.app_paackage);
            ImageView iconview = (ImageView) view.findViewById(R.id.app_icon);

            appName.setText(data.loadLabel(packageManager));
            packageName.setText(data.packageName);
            iconview.setImageDrawable(data.loadIcon(packageManager));
        }
        return view;
    }
};

MainActivity

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends ListActivity {
    private PackageManager packageManager = null;
    private List<ApplicationInfo> applist = null;
    private ApplicationAdapter listadaptor = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_eclair);

        packageManager = getPackageManager();


        new LoadApplications().execute();


    Button bottone1 = (Button)findViewById(R.id.button1);
    bottone1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            new LoadApplications().execute();


        }
    });};

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        ApplicationInfo app = applist.get(position);
       /*
        try {
            Intent intent = packageManager
                    .getLaunchIntentForPackage(app.packageName);

            if (null != intent) {
                startActivity(intent);
            }
        } catch (ActivityNotFoundException e) {
            Toast.makeText(Activity_Eclair.this, e.getMessage(),
                    Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Toast.makeText(Activity_Eclair.this, e.getMessage(),
                    Toast.LENGTH_LONG).show();
        }
        */
        Uri packageUri = Uri.parse("package:"+app.packageName);
        Intent uninstallIntent =
          new Intent(Intent.ACTION_DELETE, packageUri);
        startActivity(uninstallIntent);
    }



    private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list) {
        ArrayList<ApplicationInfo> applist = new ArrayList<ApplicationInfo>();
        for (ApplicationInfo info : list) {
            try {
                if (null != packageManager.getLaunchIntentForPackage(info.packageName)) {
                    applist.add(info);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return applist;
    }


    private class LoadApplications extends AsyncTask<Void, Void, Void> {        

        public ProgressDialog progress = null;

        @Override
        protected Void doInBackground(Void... params) {
            applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));
            listadaptor = new ApplicationAdapter(Activity_Eclair.this,
                    R.layout.snippet_list_row, applist);

            return null;
        }

        @Override
        protected void onCancelled() {
            super.onCancelled();
        }

        protected void onDestroy() {
            if(progress!=null)
                if(progress.isShowing()){
                progress.dismiss();
                }

        }

        @Override
        protected void onPostExecute(Void result) {
            setListAdapter(listadaptor);
            progress.dismiss();
            super.onPostExecute(result);
        }

        @Override
        protected void onPreExecute() {
            progress = ProgressDialog.show(Activity_Eclair.this, null,
                    "Caricamento applicazioni in corso...");
            super.onPreExecute();
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }
    }
}

When I run my application the scroll is very slow, how can I do to make it go smoother without jamming? Any kind of help is welcome. Thanks in advance.

  • 1
    I think you need to post this question on [Code Review](http://codereview.stackexchange.com/). It seems to be off-topic on SO. – Rahul Oct 25 '13 at 16:32
  • why do you override getItem, getItemId and getCount, only to replace them with identical code ? (you don't need to keep a local list either, you can rely on the list kept by the arrayadapter) – njzk2 Oct 25 '13 at 16:41
  • One thing you can do is use the Holder pattern, to avoid calling findViwById all the time. – njzk2 Oct 25 '13 at 16:42
  • you can statically load the label and icon beforehand, as it can take some time. – njzk2 Oct 25 '13 at 16:43
  • Are you running it on an emulator? That can really slow it down vs. a real device. – Embattled Swag Oct 25 '13 at 16:43
  • No, I'm not testing in emulator but on a real phone. – Simone Piglietti Oct 25 '13 at 18:11

2 Answers2

2

Well, one quick fix I can help you with right now is using a static holder in your getView() method in your adapter. Do the following so that it recycles more efficiently instead of creating new views every time.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    Holder holder;
    if (null == view) {
        LayoutInflater layoutInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = layoutInflater.inflate(R.layout.snippet_list_row, null);

        holder = new Holder();
        holder.appName = (TextView) view.findViewById(R.id.app_name);
        holder.packageName = (TextView) view.findViewById(R.id.app_paackage);
        holder.iconview = (ImageView) view.findViewById(R.id.app_icon);

        view.setTag(holder);
    }
    else
    {
        holder = (Holder)view.getTag();
    }

    ApplicationInfo data = appsList.get(position);
    if (null != data) {
        holder.appName.setText(data.loadLabel(packageManager));
        holder.packageName.setText(data.packageName);
        holder.iconview.setImageDrawable(data.loadIcon(packageManager));
    }
    return view;
}

static class Holder
{
    TextView appName, packageName;
    ImageView iconview;
}

I don't know exactly how much faster this will make your app, but it could help and this is also a common practice way to implement Adapters, so it'd be a good habit to start doing.

Hope this helps. Good luck!

Andrew Schuster
  • 3,229
  • 2
  • 21
  • 32
  • Unfortunately still not fluid. Other ideas? – Simone Piglietti Oct 25 '13 at 18:10
  • Any further optimization techniques for this particular set would be a bit out of my expertise, but I recommend taking a look at the [link](http://stackoverflow.com/questions/541966/how-do-i-do-a-lazy-load-of-images-in-listview) that tyczj posted. The images are probably what's holding you back. – Andrew Schuster Oct 25 '13 at 18:23
2

the sloweness you are seeing is probably due to the application image fetch.

you should look at this Lazy load of images in ListView

using a ViewHolder could also help too

http://www.itworld.com/development/380008/how-make-smooth-scrolling-listviews-android

Community
  • 1
  • 1
tyczj
  • 71,600
  • 54
  • 194
  • 296