Mate Križanac
Mate Križanac

Reputation: 268

Android ArrayAdapter slow scrolling

i am building an app that reads images from url then puts them in a list view with some text, i created custom adapter layout and everything, and it works, it pusts everything in a list view as imagined but when i scroll down it is laging a lot, any ideas how to get rid of lag ?

here is my ArrayAdapter code:

public class PhotoAdapter extends ArrayAdapter<PhotoInst> {

private Context con;
private ArrayList<PhotoInst> photos;
private int resource;

public PhotoAdapter(Context context, int textViewResourceId,
        ArrayList<PhotoInst> objects) {
    super(context, textViewResourceId, objects);
    // TODO Auto-generated constructor stub

    con = context;
    resource = textViewResourceId;
    photos = objects;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {



    View rowView =convertView;

    if (rowView==null) {

    LayoutInflater inflater = (LayoutInflater) con
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    rowView = inflater.inflate(resource, parent, false);
    }

    ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView1);
    TextView textView = (TextView) rowView.findViewById(R.id.textView1);
    TextView caption = (TextView) rowView.findViewById(R.id.textView2);

    String url = photos.get(position).getPicUrl();
    String name = photos.get(position).getName();
    String captionText = photos.get(position).getCaption();

    Bitmap myPic = DownloadImage(url);

    imageView.setImageBitmap(myPic);

    textView.setText(name);
    caption.setText(captionText);

    return rowView;

}

private Bitmap DownloadImage(String URL) {

    Bitmap bitmap = null;
    InputStream in = null;
    try {
        in = OpenHttpConnection(URL);
        bitmap = BitmapFactory.decodeStream(in);
        in.close();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    return bitmap;
}

private InputStream OpenHttpConnection(String urlString) throws IOException {
    InputStream in = null;
    int response = -1;

    URL url = new URL(urlString);
    URLConnection conn = url.openConnection();

    if (!(conn instanceof HttpURLConnection))
        throw new IOException("Not an HTTP connection");

    try {
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("GET");
        httpConn.connect();

        response = httpConn.getResponseCode();
        if (response == HttpURLConnection.HTTP_OK) {
            in = httpConn.getInputStream();
        }
    } catch (Exception ex) {
        throw new IOException("Error connecting");
    }
    return in;
}

}

Upvotes: 0

Views: 469

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007124

You are downloading images on the main application thread. This is not only going to slow your UI down dramatically, but your app will crash on Android 4.0+ with a NetworkOnMainThreadException.

Use Picasso or SmartImageView or something to download your images asynchronously.

Upvotes: 2

Related Questions