Skip to content Skip to sidebar Skip to footer

Changing Gridview's Imageview Outside Of The Adapter

How can I do it? This is the method in GridView adapter public static void changeView(Bitmap bmp, int pos){ GridView gridView = new GridView(mContext); ImageView vie

Solution 1:

make a collection in your adapter to hold the images that you need applied. When you need to change the image of a specific getView row, change in the image in the corresponding collection:

private Bitmap[] imgCollection;

@Overridepublic View getView(int position, View convertView, ViewGroup parent) {

    ImageView imageView;
    if (convertView == null) {  
        //  imageView = Inflater.inflate(R.layout.image_layout, null);
        imageView = newImageView(mContext);
        if (DefinedValues.width/3 < DefinedValues.height/4)
            imageView.setLayoutParams(newGridView.LayoutParams
                    (DefinedValues.height/4, DefinedValues.height/4));
        else
            imageView.setLayoutParams(newGridView.LayoutParams
                    (DefinedValues.width/3-5, DefinedValues.width/3-5));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }
    imageView.setImageBitmap(imgCollection[position]); // <-- changed return imageView;
}


publicvoidchangeImageBitmap(Bitmap bmp, int pos) {
    imgCollection[pos] = bmp;
    notifyDataSetChanged(); // refresh the listview
}

moreover, if you're adverse to putting redundant bitmaps in a collection just to load them as a default value you could manage with this in getView instead:

    if (imgCollection[position] == null) {
        imageView.setImageResource(R.drawable.test3);
    } else {
        imageView.setImageBitmap(imgCollection[position]); // <-- changed           
    }

is it ok to set Context object mContext in class static?

No absolutely not. there should never be a need to do this. if the context is static, it could stop your activity or anything in it from being garbage collected. Your app might eventually crash from lack of resources.

Post a Comment for "Changing Gridview's Imageview Outside Of The Adapter"