2011-04-01 70 views
1

我感到有點困惑這段代碼:膨脹一個視圖分配一個新的對象?

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

線圖像是一個簡單的線程裏面做一個下載的圖像,並採取2 PARAM:

1)ImageView的地方線程調用setImage(位圖)

2)表示URL(下載圖像的字符串)

的問題是,我可以看到我所有的照片閃爍,當單個圖像被下載,所有的照片具有相同的圖像(URL AR不一樣)。

這是正確的方法嗎?爲每個元素填充一個NEW視圖爲每個迭代創建一個NEW VIEW OBJECT或者它們是相同的對象?

怎麼了?

非常感謝。

pedr0

這是我的線程的代碼:

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



} 

回答

0

你也應該張貼線程代碼。在Android中,不能從UI線程以外的其他線程更改UI對象(ImageView)。這可能是正在發生的事情。

如果您有多個視圖,則應考慮使用ListView小部件以及自定義適配器(以擴大每行的佈局)。


編輯:

  1. 您是否嘗試過在第一次擺脫了動畫,看看問題是否仍然存在?

  2. 我還注意到你正在創建兩個ImageViewURL對象。

    image[i] = new ImageViewURL(commento_foto, item.getAnteprimaURL()); 
        thread_image_commenti = new ImageThreadURL(this); 
        thread_image_commenti.execute(new ImageViewURL(commento_foto, item.getAnteprimaURL())); 
    

難道不應該是:

 image[i] = new ImageViewURL(commento_foto, item.getAnteprimaURL()); 
     new ImageThreadURL(this).execute(image[i]); 

+0

太長我覺得,我在onProgressUpdate()中執行setImage代碼,它在UI線程中執行。 – pedr0 2011-04-01 08:21:22

+0

也看到我編輯的答案 – 2011-04-01 08:25:06

+0

有一個沒有問題的動畫做了這個閃爍效果。 :-) 謝謝! – pedr0 2011-04-01 08:33:53