带有来自URL的图像的Android gridview

时间:2015-09-08 10:56:41

标签: android image gridview

我正在尝试使用从网络加载的拇指图像构建网格视图。 我有一个url数组,然后我传递给适配器customgrid(代码将跟随)。 位图的下载是通过异步任务完成的。 最终的结果是,在15张图片中,只有2张实际显示,而其他13张没有显示(但它们都存在,我尝试使用相同的网址进行所有这些仍然会发生。 - 查看附加图片。 How it looks currently

没有任何错误被抛出。 任何帮助将不胜感激。

CustomGrid.Java类:

public class CustomGrid extends BaseAdapter{

private Context mContext;
private final String[] web;
private final String[] ImageUrl;
ImageView imageView;
Bitmap bitmap;



public CustomGrid(Context c,String[] web,String[] ImageUrl ) {
    mContext = c;
    this.ImageUrl = ImageUrl;
    this.web = web;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return web.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View grid;
    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        grid = new View(mContext);
        grid = inflater.inflate(R.layout.gallery_item, null);
        imageView = (ImageView)grid.findViewById(R.id.grid_image);

        try
        {
            new LoadImage().execute(ImageUrl[position]);

        }catch (Exception e) {

            e.printStackTrace();
        }


    } else {
        grid = (View) convertView;
    }

    return grid;
}

private class LoadImage extends AsyncTask<String, String, Bitmap> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();


    }

    protected Bitmap doInBackground(String... args) {
        try {
            bitmap = BitmapFactory.decodeStream((InputStream)new URL(args[0]).getContent());

        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    protected void onPostExecute(Bitmap image) {


        if(image != null){
            imageView.setImageBitmap(image);
        }else{

        }
    }
}

}

加载图片的主要活动代码:(网页只是一个字符串数组,而ImageUrl是一个带有图片网址的数组)

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getLayoutInflater().inflate(R.layout.activity_trips_and_activities, frameLayout);

    CustomGrid adapter = new CustomGrid(TripsAndActivities.this, web, imageUrl);
    grid=(GridView)findViewById(R.id.grid);
    grid.setAdapter(adapter);
    grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            Toast.makeText(TripsAndActivities.this, "You Clicked at " + web[+position], Toast.LENGTH_SHORT).show();

        }
    });

}

1 个答案:

答案 0 :(得分:0)

您遇到的问题是您的imageView变量正在发生变化(在循环期间)并使图像异步。

到达图像时,imageView指向不同的元素。

尝试以下

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

    public BitmapWorkerTask(ImageView imageView) {
        // Use a WeakReference to ensure the ImageView can be garbage collected
        imageViewReference = new WeakReference<ImageView>(imageView);
    }

    // Decode image in background.
    @Override
    protected Bitmap doInBackground(String... args) {
       try {
           bitmap = BitmapFactory.decodeStream((InputStream)new URL(args[0]).getContent());

        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    // Once complete, see if ImageView is still around and set bitmap.
    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (imageViewReference != null && bitmap != null) {
            final ImageView imageView = imageViewReference.get();
            if (imageView != null) {
                imageView.setImageBitmap(bitmap);
            }
        }
    }
}

然后你需要像这样使用它

  BitmapWorkerTask task = new BitmapWorkerTask(imageView);
  task.execute(ImageUrl[position]);

或者你现在写的

new BitmapWorkerTask(imageView).execute(ImageUrl[position]);

另外,将GetView重构为此类

var view = convertView;
if (view == null) {
    view = inflater.inflate(R.layout.gallery_item, null);
} 

var myImageView = (ImageView)view.findViewById(R.id.grid_image);

try {
    //You can further optimize the code, by storing the bitmap is some
    //cache, so when the user scrolls away, it doesn't throw the image 
    //away but keeps it for reuse once they scroll back.
    new LoadImage(myImageView).execute(ImageUrl[position]);
} catch (Exception e) {
    e.printStackTrace();
}

return view;