1

I have a ListView with an image in every Item. I download the images from Server. As there are more than two images the loading gets really slow and as you scroll down you see the image from above and have to wait long until it changes to the correct image. I already tried to use the tips from this post: http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/ but it's still slow. What would be the best and easiest method to improve this. Here is the Code from my BaseAdapter Class:

public class GetAllEntrysListViewAdapter extends BaseAdapter {

private JSONArray dataArray;
private Activity activity;
private static LayoutInflater inflater = null;
public Cursor cursor;
private SQLiteDatabase dbase;
DbHelper dbh;
private Context context;
String pos;
Integer markerID;
public String objectID;



private static final String baseUrlForImage = "http://...";

public GetAllEntrysListViewAdapter(JSONArray jsonArray, Context context, Integer markerID) {
    this.dataArray = jsonArray;
    this.context= context;
    this.markerID = markerID;
    inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

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

    final ListCell cell;
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.get_all_entry_list_view_cell, null);

        cell = new ListCell();

        cell.likes = (TextView) convertView.findViewById(R.id.listViewLikes);
        cell.note = (TextView) convertView.findViewById(R.id.listViewNote);
        cell.img = (ImageView) convertView.findViewById(R.id.listViewImg);
        cell.likeImage = (ImageView) convertView.findViewById(R.id.heartImage);

        convertView.setTag(cell);

    }
    else {
        cell = (ListCell)convertView.getTag();
    }

    cell.position = position;

    try {
        JSONObject jsonObject = this.dataArray.getJSONObject(position);
        cell.likes.setText(jsonObject.getString("likes"));
        cell.note.setText(jsonObject.getString("note"));
        cell.entryID = jsonObject.getString("id");
        String img = jsonObject.getString("image");
        String urlForImageInServer = baseUrlForImage + img;

        new AsyncTask<String, Void, Bitmap>() {
            private int mPosition = position;
            private ListCell mCell = cell;
            @Override
            protected Bitmap doInBackground(String... params) {
                //download image
                String url = params[0];
                Bitmap icon = null;

                try {
                    InputStream in = new java.net.URL(url).openStream();
                    icon = BitmapFactory.decodeStream(in);
                } catch(MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                return icon;
            }

            @Override
            protected void onPostExecute(Bitmap result) {
                if (cell.position == mPosition) {
                    cell.img.setImageBitmap(result);
                }
            }

        }.execute(urlForImageInServer);


    catch (JSONException e) {
        e.printStackTrace();
    }
    return convertView;

}

public static class ListCell {
    private TextView likes;
    private TextView note;
    private ImageView img;
    public ImageView likeImage;
    public int position;
    public String entryID;
}

}

maidi
  • 3,219
  • 6
  • 27
  • 55

3 Answers3

2

Do not reinvent the wheel. Don't create AsyncTask for every item of your ListView. Use some library which will download, cache and help to display your images like Picasso:

Picasso.with(context).load("file:///android_asset/DvpvklR.png").into(imageView2);

Also it is superfluous to split up your JSONObject to separate params for every item of your ListView. Create array of POJO objects but not just array of JSONObject. Or if your ListView shows only images then you can pass to the adapter just array of String.

PS And try to find and read fresh articles. You mentioned article of 2012 year.

eleven
  • 6,779
  • 2
  • 32
  • 52
0

By far the most popular SO post is @ Lazy load of images in ListView. It mentions Picasso as well as other packages. Have fun...

Community
  • 1
  • 1
The Original Android
  • 6,147
  • 3
  • 26
  • 31
0

You can use Volley Library it will relax you and do everything

http://www.androidhive.info/2014/07/android-custom-listview-with-image-and-text-using-volley/

     <!-- Thumbnail Image -->
        <com.android.volley.toolbox.NetworkImageView
            android:id="@+id/thumbnail"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_alignParentLeft="true"
            android:layout_marginRight="8dp" />

    NetworkImageView thumbNail = (NetworkImageView) convertView
                .findViewById(R.id.thumbnail);

   // thumbnail image
        thumbNail.setImageUrl(Your url, imageLoader);
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41