Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android gridview使用异步任务图像加载问题_Android_Image_Gridview_Android Asynctask - Fatal编程技术网

Android gridview使用异步任务图像加载问题

Android gridview使用异步任务图像加载问题,android,image,gridview,android-asynctask,Android,Image,Gridview,Android Asynctask,我是android新手。我使用AsyncTask在gridview中显示图像。但也存在一些问题,如: 1:onScroll AsyncTask再次收到调用。 2:图像显示不正确(一旦我滚动显示顶部图像并加载原始图像) 这是我的密码: public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater layoutInflater = (LayoutInfla

我是android新手。我使用AsyncTask在gridview中显示图像。但也存在一些问题,如:

1:onScroll AsyncTask再次收到调用。 2:图像显示不正确(一旦我滚动显示顶部图像并加载原始图像)

这是我的密码:

    public View getView(int position, View convertView, ViewGroup parent) {

            LayoutInflater layoutInflater = (LayoutInflater) ctx
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            ListRowHolder listRowHolder;
            if (convertView == null) {
                convertView = layoutInflater.inflate(R.layout.ll_sponsor_list_item,
                        parent, false);
                listRowHolder = new ListRowHolder();
                listRowHolder.imgSponsor = (ImageView) convertView
                        .findViewById(R.id.imggrid_item_image);
                convertView.setTag(listRowHolder);

            } else {
                listRowHolder = (ListRowHolder) convertView.getTag();
            }
            try {
                task = new BitmapWorkerTask(listRowHolder.imgSponsor);
                task.image_path = ImageName.get(position);
                task.execute(1);

            } catch (Exception e) {
                if (thisLogger != null) {
                    thisLogger.error(e.toString());
                    thisLogger.error("\r\n");
                }
            }

            return convertView;
        }



class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
        private final WeakReference<ImageView> imageViewReference;

        private String image_path;

        public BitmapWorkerTask(ImageView imageView) {
            imageViewReference = new WeakReference<ImageView>(imageView);
        }



@Override
    protected Bitmap doInBackground(Integer... params) {

        try {
            while (running) {
                Bitmap picture = BitmapFactory.decodeFile(image_path);
                int width = picture.getWidth();
                int height = picture.getHeight();
                float aspectRatio = (float) width / (float) height;
                int newWidth = 98;
                int newHeight = (int) (98 / aspectRatio);
                Log.v("ImageList", "L");
                return picture = Bitmap.createScaledBitmap(picture,
                        newWidth, newHeight, true);

            } 
        } catch (Exception e) {
            if (thisLogger != null) {
                thisLogger.error(e.toString());
                thisLogger.error("\r\n");
            }
        }
        return null;

    }


    @Override
            protected void onPostExecute(Bitmap bitmap) {
                if (imageViewReference != null && bitmap != null) {
                    final ImageView imageView = imageViewReference.get();
                    if (imageView != null) {
                        imageView.setImageBitmap(bitmap);
                    }

                }

            }
public View getView(int位置、视图转换视图、视图组父视图){
LayoutInflater LayoutInflater=(LayoutInflater)ctx
.getSystemService(上下文布局\充气机\服务);
ListRowHolder ListRowHolder;
if(convertView==null){
convertView=LayoutFlater.充气(R.layout.ll\u赞助商\u列表\u项目,
父母,假);
listRowHolder=新listRowHolder();
listRowHolder.imgSponsor=(图像视图)convertView
.findviewbyd(R.id.imggrid_项目_图像);
convertView.setTag(listRowHolder);
}否则{
listRowHolder=(listRowHolder)convertView.getTag();
}
试一试{
任务=新的位图工作任务(listRowHolder.imgSponsor);
task.image_path=ImageName.get(位置);
任务执行(1);
}捕获(例外e){
如果(thisLogger!=null){
thisLogger.error(例如toString());
thisLogger.error(“\r\n”);
}
}
返回视图;
}
类BitmapWorkerTask扩展了AsyncTask{
私有最终WeakReference imageViewReference;
私有字符串图像路径;
公共位图工作任务(ImageView ImageView){
imageViewReference=新的WeakReference(imageView);
}
@凌驾
受保护位图doInBackground(整数…参数){
试一试{
(跑步时){
位图图片=位图工厂.decodeFile(图像路径);
int width=picture.getWidth();
int height=picture.getHeight();
浮动纵横比=(浮动)宽度/(浮动)高度;
int newWidth=98;
int newHeight=(int)(98/aspectRatio);
Log.v(“图像列表”、“L”);
返回图片=位图。createScaledBitmap(图片,
newWidth、newHeight、true);
} 
}捕获(例外e){
如果(thisLogger!=null){
thisLogger.error(例如toString());
thisLogger.error(“\r\n”);
}
}
返回null;
}
@凌驾
受保护的void onPostExecute(位图){
if(imageViewReference!=null&&bitmap!=null){
最终ImageView=imageViewReference.get();
如果(imageView!=null){
设置图像位图(位图);
}
}
}

请让我知道这里出了什么问题。

我当然会这样做。你能帮我解决这个问题吗?我正在做类似的事情!!!