图像适配器在没有光标的情况下将 ImageViews 动态添加到 GridView [英] Image Adapter adding ImageViews dynamically to GridView without Cursor

查看:15
本文介绍了图像适配器在没有光标的情况下将 ImageViews 动态添加到 GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想保持简短.我已经为 Gridview 设置了一个 ImageAdapter.不幸的是,我没有使用标准图片路径,而是使用我自己创建的一些路径.

I want to keep it short and simple. I have set up an ImageAdapter to a Gridview. Unfortunatley i am working not with the standard picture paths but with some that i created on my own.

这是我的 ImageAdapter - 在视图下你会看到 -imageView.setImageBitmap(extractThumbnail(bm, 10, 10));- 这一行只刷新当前的图像视图......我如何为每张图片创建一个图像视图,就像在构造器视图下一样循环?- 我认为 Cursor 对我不起作用,它们只能处理预定义的内容 URI.

Here is my ImageAdapter - under view you will see -imageView.setImageBitmap(extractThumbnail(bm, 10, 10)); - this line only refreshes the current image view... how can i create an image view for each picture with a loop like under Contructor View? - Cursor dont work for me I think, they only can deal with the pre defined content URI's.

我很乐意提供帮助.提前给你下油

I would be glad for some help. Tank you in Advance

private class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return 1;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        File f = new File(IMAGE_DIRECTORY);
        File[] files = f.listFiles();
        imageView.setImageURI(Uri.parse(Environment.getExternalStorageDirectory()+"/MyDir/"));
        for(int i=0; i<files.length; i++){
            Bitmap bm = BitmapFactory.decodeFile(IMAGE_DIRECTORY + File.separator + files[i].getName());

            imageView.setImageBitmap(extractThumbnail(bm, 10, 10));

        }
         return imageView;
    }
}

推荐答案

我遇到了同样的问题,并以这种方式解决了.我希望这会有所帮助.

I had the same problem and solved it this way. I hope that helps.

public class ImageAdapter extends BaseAdapter {

    private Context mContext;
    private Bitmap[]mis_fotos;

    public ImageAdapter(Context c) {
        mContext = c;    }

    public int getCount() {
        get_images();
        return mis_fotos.length;
        }

    public Object getItem(int position) {
        return null;    }

    public long getItemId(int position) {
        return 0;    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(200, 150));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(0, 0, 0, 0);
        } else {
            imageView = (ImageView) convertView;
        }
        imageView.setImageBitmap(mis_fotos[position]);
        return imageView;
    }

    private void get_images(){
        File directory = new File(Variables.PATH_FOTOS);   

        File[] archivos =directory.listFiles();
        mis_fotos= new Bitmap[archivos.length];

        for (int cont=0; cont<archivos.length;cont++){

            File imgFile = new  File(archivos[cont].toString());                
            mis_fotos[cont] = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
        }   
    }
}

这篇关于图像适配器在没有光标的情况下将 ImageViews 动态添加到 GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆