显示从dataBase到ListView的图像(使用AsyncTask)

时间:2013-09-19 01:23:05

标签: android listview bitmap android-asynctask imageview

我想正确地将图像设置为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;
        }

DownloadImage AsyncTask

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

1 个答案:

答案 0 :(得分:0)

可能是回收正在弄乱你的意见 我只是在猜这里 尝试标记您的观点。

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

使用getTag()来识别您的imageview。

        protected void onPostExecute (Bitmap image) {

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

                imv.setImageBitmap(image);}

        }

如果上述情况不会尝试LazyList非常有效。