1

我想将图像正确设置为 ListView 内的 ImageView。我使用 SimpleCursorAdapter 显示所有字段并使用 ViewBinder 将图像位图设置为 ImageView。使用 AsyncTask 下载图像。但是图像不在正确的行上。

mCurAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {


            @Override
            public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
                // TODO Auto-generated method stub
                if (view.getId()==R.id.iconPosition) {
                    Log.d("COLONNE INDEX",""+columnIndex);

                    ImageView image = (ImageView) view;



                        new DownloadImage(image).execute(cursor.getInt(columnIndex));

                    }
                    return true;
                }
                return false;
            }
        });
            this.setListAdapter(mCurAdapter);

         return view;
        }

下载图像异步任务

public class DownloadImage extends AsyncTask<Integer, Integer, Bitmap>{

            private ImageView imv;

            public DownloadImage(ImageView image){
                imv=image;
            }
            @Override
            protected Bitmap doInBackground(Integer... arg0) {
                // TODO Auto-generated method stub
                return downloadImage(arg0[0]);
            }


            protected void onPostExecute (Bitmap image) {

                if(image != null && imv != null){

                    imv.setImageBitmap(image);}

            }

            private Bitmap downloadImage(Integer res) {

                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize=8;
                Bitmap bitmap=BitmapFactory.decodeResource(getResources(),res,options);

                return bitmap;
                }
        }
4

1 回答 1

0

可能是回收扰乱了你的观点。
我只是在这里猜测。
尝试标记您的视图。

ImageView image = (ImageView) view;
image.setTag("row2");

并使用 getTag() 来识别您的图像视图。

        protected void onPostExecute (Bitmap image) {

            if(image != null && imv != null && imv.gettag().equals("row2")){

                imv.setImageBitmap(image);}

        }

如果上面没有尝试非常有效的LazyList 。

于 2013-09-19T04:56:54.857 回答