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;
}
}