膨胀视图分配新对象?

时间:2011-04-01 07:51:24

标签: android view

我对这段代码感到有些困惑:

  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个参数:

1)一个ImageView,其中线程调用setImage(Bitmap)

2)表示URL(用于下载图像)的字符串

问题在于,当下载单张图片并且所有照片都具有相同的图像(URL不相同)时,我可以看到所有照片都闪烁。

这是正确的方法吗?为每个元素膨胀一个新视图为每个迭代创建一个新的视图对象,或者它们是同一个对象?

怎么了?

提前多多感谢。

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



}

1 个答案:

答案 0 :(得分: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()));
    
  3. 不应该是:

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