Skip to content Skip to sidebar Skip to footer

Gridview With Custom Imageadapter Doesn't Update After Dataset Changes

I'm implementing a gallery of images with support for multiple item selection. The main layout is a GridView which uses a custom Adapter. Every displayed item is a combination Imag

Solution 1:

The problem is mostly likely that your getView() method correctly initialises a new cell view but never updates the state of a cell view that has been recycled. So essentially each cell view will only ever display the state it was initialised to. It should look something like this:

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

    if (cell == null) {
        // No View passed, create one.
        cell = mInflater.inflate(R.layout.galleryitem, null);
        // Setup the View contentImageViewimageView= (ImageView)cell.findViewById(R.id.thumbImage);
        CheckBoxcheckBox= (CheckBox) cell.findViewById(R.id.itemCheckBox);
        checkBox.setChecked(checkboxesState[position]);
        checkBox.setId(position);
        imageView.setImageBitmap(downsample(mThumbIds[position]));

        // Setup the View behavior
        imageView.setOnClickListener(newOnClickListener() {
            publicvoidonClick(View v) {
                // Uncheck all checked items
                uncheckAll();
            }
        });

        checkBox.setOnClickListener(newOnClickListener() {
            publicvoidonClick(View v) {
                CheckBoxcb= (CheckBox) v;
                checkboxesState[cb.getId()] = cb.isChecked();
            }
        });
    } else {
        ImageViewimageView= (ImageView)cell.findViewById(R.id.thumbImage);
        CheckBoxcheckBox= (CheckBox) cell.findViewById(R.id.itemCheckBox);
        checkBox.setChecked(checkboxesState[position]);
        checkBox.setId(position);
        imageView.setImageBitmap(downsample(mThumbIds[position]));
    }

    return cell;
}

Additionally to improve the performance of your getView() method take a look at this tutorial.

Post a Comment for "Gridview With Custom Imageadapter Doesn't Update After Dataset Changes"