I am a little bit confused about this piece of code :
for(int i = 0; i < poi.getCommenti().size();i++){
item = poi.getCommenti().get(i);
commento = li.inflate(R.layout.commento, null);
commento_data = (TextView) commento.findViewById(R.id.commento_data);
commento_descrizione =(TextView) commento.findViewById(R.id.commento_descrizione);
commento_name = (TextView) commento.findViewById(R.id.commento_nome);
commento_foto = (ImageView) commento.findViewById(R.id.commento_foto);
Log.d(TAG, "commento_foto:"+commento_foto);
commento_data.setText(item.getData());
commento_descrizione.setText(item.getTesto());
commento_name.setText(item.getUtente().getName());
contenitore_commenti.addView(commento);
image[i] = new ImageViewURL(commento_foto, item.getAnteprimaURL());
}
// I start only one thread for all images
thread_image_commenti = new ImageThreadURL(this);
thread_image_commenti.execute(image);
thread image is a simple thread which do a download of an image and take 2 param:
1) an ImageView where thread call setImage(Bitmap)
2) an String representing an URL (for download image)
The problem is that I can see all my photo flicker when a single image is downloaded and all photo have the same image (URL are not the same).
This is the right way ? Inflate a NEW view for each element create a NEW VIEW OBJECT for each iteration or them are the same object ?
What’s wrong?
Thanks a lot in advance.
pedr0
This is the code of my thread:
public class ImageThreadURL extends AsyncTask<SingleImageURL, SingleImageURL,Void>{
public final static String TAG = "ImageThreadURL";
static final int MAX_CONN = 25;
static final int TIMEOUT_DATA = 0;
static final int TIMEOUT_CONNECTION = 10000;
Activity caller;
Animation animation;
public ImageThreadURL(Activity caller) {
super();
this.caller = caller;
this.animation = AnimationUtils.loadAnimation(caller, R.anim.alphaanimation);
animation.setRepeatCount(0);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
caller.setProgressBarIndeterminateVisibility(true);
}
@Override
protected Void doInBackground(SingleImageURL... arg0) {
Log.d(TAG, "Lunghezza array input:"+arg0.length);
HttpParams parameters = new BasicHttpParams();
HttpProtocolParams.setVersion(parameters, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(parameters, HTTP.UTF_8);
HttpProtocolParams.setUseExpectContinue(parameters, false); // some webservers have problems if this is set to true
ConnManagerParams.setMaxTotalConnections(parameters, MAX_CONN);
HttpConnectionParams.setConnectionTimeout(parameters, TIMEOUT_CONNECTION);
HttpConnectionParams.setSoTimeout(parameters,TIMEOUT_DATA);
SchemeRegistry schReg = new SchemeRegistry();
schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
ClientConnectionManager conMgr = new ThreadSafeClientConnManager(parameters,schReg);
DefaultHttpClient client_http = new DefaultHttpClient(conMgr, parameters);
HttpGet method;
HttpResponse response;
HttpEntity entity;
InputStream in ;
Bitmap image ;
SingleImageURL actual=null;
try{
for(int i = 0; i < arg0.length ;i++){
actual = arg0[i];
Log.d(TAG,"Preparo download:"+ actual.getUrl());
if(actual.getUrl() == null)
continue;
method = new HttpGet(Uri.decode(actual.getUrl()));
response = client_http.execute(method);
entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
in = bufHttpEntity.getContent();
image = BitmapFactory.decodeStream(in);
// problema scaricamento immagine
if(image == null){throw new Exception("BitmapFactory.decodeStream() return null!");}
else{
Log.d(TAG,"Eseguito Download:"+ actual.getUrl());
actual.setImage(image);
publishProgress(arg0[i]);
}
}
}catch (Exception e) {e.printStackTrace();}
return null;
}
@Override
protected void onProgressUpdate(SingleImageURL... values) {
Log.d(TAG, "onProgressUpdate");
values[0].getView().startAnimation(animation);
values[0].setImage();
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
caller.setProgressBarIndeterminateVisibility(false);
}
}
You should also post the Thread code. In Android, you cannot alter UI objects (ImageView) from another thread than the UI thread. This might be what is happening.
If you have multiple views, you should consider using the ListView widget along with a custom adapter (to inflate the layout for each row).
Edit:
Have you tried to get rid of the animations at first time to see if the problem remains?
I also noticed that you are creating two ImageViewURL objects.
Shouldn’t that be:
?