Android - Set The Source Of An Imageview Inside A Custom Listview Adapter From The Getview Function Does Not Work Properly
Apologies for the long title. I am building an Android application where an activity contains a custom listView. In the layout of my custom listView, I have an image view and thre
Solution 1:
the reason it's not working it's because dealing with image download, cache and view recycling is way way more complex than what your code is handling.
I can point it out a few things I can see it's wrong from your code, but for sure there's more:
- there's no RAM cache, that means the moment the user scrolls a view out if view and then back into view, the image will be downloaded again from the server.
- there's no disk cache, that means that if the user scrolls too far in a list, or if he/she leaves the app for a few minutes to later come back, the image will be downloaded again from the server
- on this line
holder.photo.setImageBitmap
you're trying to change aView
in a background thread. - AsyncTask (starting on some API) is a mono-thread class, meaning all your downloads will be queue in happen one after the other, so probably the user will be staring at a blank screen for a while before seeing anything.
- you're not canceling the task, so during recycling, the call
setImageBitmap
might be happening on the wrong item.
replace the line:
new setDisplayPhoto().execute(listData.get(position).getPicture_path());
with:
Picasso.with(convertView.getContext())
.load("http://192.168.0.10:8888/server/path/here/" +
listData.get(position).getPicture_path())
.into(holder.photo);
for that to work you'll need one extra libary that you can add by adding the following line on your manifest:
compile'com.squareup.picasso:picasso:2.5.2'
for more info on the library check this: http://square.github.io/picasso/
Solution 2:
package com.example.jojo.gridview;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
publicclassGirdViewAdapterextendsBaseAdapter {
private Activity activity;
private ArrayList<String> ar;
private LayoutInflater inflater;
publicGirdViewAdapter(ArrayList<String> ar, Activity con)
{
activity = con;
this.ar=ar;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
privateclassViewHolder {
public TextView textView;
public ImageView imageView;
}
@OverridepublicintgetCount() {
// TODO Auto-generated method stubreturn ar.size();
}
@Overridepublic Object getItem(int arg0) {
// TODO Auto-generated method stubreturnnull;
}
@OverridepubliclonggetItemId(int arg0) {
// TODO Auto-generated method stubreturn0;
}
@Overridepublic View getView(int position, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stubViewrowView= arg1;
ViewHolder viewHolder;
if (arg1 == null) {
rowView = inflater.inflate(R.layout.style_view, null);
viewHolder = newViewHolder();
viewHolder.textView = (TextView) rowView.findViewById(R.id.textView);
viewHolder.imageView = (ImageView) rowView.findViewById(R.id.imageView);
rowView.setTag(viewHolder);
} elseviewHolder= (ViewHolder) rowView.getTag();
String rowData[] = this.ar.get(position).split("\\|");
viewHolder.textView.setText(rowData[0] );
setImageView(viewHolder.imageView, rowData[1] );
return rowView;
}
privatevoidsetImageView(ImageView imageView, String url) {
classImageDownloadHelperextendsAsyncTask<String, String, Bitmap> {
@Overrideprotected Bitmap doInBackground(String... params) {
// TODO Auto-generated method stub
URL url;
String_url= params[0];
Log.e("url",_url );
BufferedOutputStream out;
InputStream in;
BufferedInputStream buf;
try {
url = newURL(_url);
in = url.openStream();
buf = newBufferedInputStream(in);
BitmapbMap= BitmapFactory.decodeStream(buf);
if (in != null) {
in.close();
}
if (buf != null) {
buf.close();
}
return bMap;
} catch (Exception e) {
Log.e("Error reading file", e.toString());
}
returnnull;
}
}
ImageDownloadHelperdownloadHelper=newImageDownloadHelper();
downloadHelper.execute(url);
try {
imageView.setImageBitmap(downloadHelper.get());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Post a Comment for "Android - Set The Source Of An Imageview Inside A Custom Listview Adapter From The Getview Function Does Not Work Properly"