【问题标题】:Display Image from dataBase to ListView ( with AsyncTask )将图像从数据库显示到 ListView(使用 AsyncTask )
【发布时间】:2013-09-19 01:23:05
【问题描述】:

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

【问题讨论】:

    标签: android listview bitmap android-asynctask imageview


    【解决方案1】:

    可能是回收与您的观点发生了冲突。
    我只是在这里猜测。
    尝试标记您的视图。

    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,这是非常有效的。

    【讨论】:

    • 它不起作用,当我在 listView 中导航时图像发生变化。但是当我不使用 Asynctask 时,它正在工作
    • 你遇到了回收问题,试试lazylist github.com/thest1/LazyList
    • LazyList 使用 URL 和 BaseAdapter,但我使用 drawable 和 SimpleCursorAdapter,所以我不能使用 LazyList
    • 我更改了我的应用程序以从 url 检索图像并且它有效。谢谢懒惰的忍者:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-31
    • 2013-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多