GridView: getCheckedItemPositions() return no values or wrong values

I implemented a gallery with a Grid View that includes check boxes but i have problems with getCheckedItemPositions() method.

(1) If i launch the Activity with GridView, select some items and ask which items have been selected, the method getCheckedItemPositions() doesn't return any value.

(2) If i launch another Activity, i return to Activity with the GridView and ask what items have been selected, the method getCheckedItemPositions() returns wrong values .

I think first of all that the implementation of getView() is wrong, for (1) probably the GridView doesn't know which items are selected. I have no ideas for the (2) instead.

Here the code:

GalleryFragment getCheckedItemOnGridView():

private void getCheckedItemOnGridView() {
    if(D) Log.d(TAG, "getCheckedItemOnGridView(): called");
    SparseBooleanArray checkedItemPositions = mGalleryGridView.getCheckedItemPositions();
    for (int i=0 ; i<checkedItemPositions.size() ; i++) {
        if(D) Log.d(TAG, "checkedItemPositions : " + checkedItemPositions.valueAt(i) + " index " + checkedItemPositions.keyAt(i));
    }
}

Adapter extends BaseAdapter getView():

private SparseBooleanArray checked;

public View getView(final int position, View convertView, ViewGroup parent) {
        if(D) Log.d(TAG, "Called: getView");
    ViewHolder holder;

    if (convertView == null) {
        convertView = mLayoutInflater.inflate(R.layout.listitem_gallery, null);

        holder = new ViewHolder();
        holder.thumbnailView = (ImageView) convertView.findViewById(R.id.imageview_thumbnail);
        holder.checkBoxView = (CheckBox) convertView.findViewById(R.id.checkbox);

        holder.checkBoxView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (((CheckBox) view).isChecked()) {
                    checked.put((Integer) view.getTag(), true);

                } else {
                    checked.put((Integer) view.getTag(), false);
                }
            }
        });

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    BitmapFileImageLoader bitmapLoader = new BitmapFileImageLoader();
    bitmapLoader.loadBitmap(getItem(position), holder.thumbnailView);

    holder.checkBoxView.setTag(position);
    holder.checkBoxView.setChecked(checked.get(position));

    return convertView;
}

Answers


I don't think the problem is with OnClickListener of CheckBox. but you could try setting OnCheckedChangeListener instead of OnClickListener...

    holder.checkBoxView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            int position = (Integer) buttonView.getTag();
            checked.put(position, isChecked); 
        }
    });

Found new solution "workaround". The two solutions above didn't resolve the problem, so i decided to use directly the SparseBooleanArray used in the Adapter to solve the problem of check boxes with the view recycled. Here the code in Gallery Fragment:

private List<Resource> getCheckedItemOnGridView() {

    if(D) Log.d(TAG, "getCheckedItemOnGridView(): called");

    List<Resource> resources = new ArrayList<Resource>();
    // List of checked checkbox
    SparseBooleanArray checkedItemPositions = mAdapter.getCheckedItemPositions();

    for (int i=0 ; i<checkedItemPositions.size() ; i++) {
        if(checkedItemPositions.valueAt(i)) {
            if(D) Log.d(TAG, "getCheckedItemOnGridView(): item position checked: " + checkedItemPositions.keyAt(i));
            resources.add(mResources.get(checkedItemPositions.keyAt(i)));
        }
    }
    return (resources.isEmpty()) ? null : resources;
}

Need Your Help

recognize Ruby code in Treetop grammar

ruby parsing grammar erb treetop

I'm trying to use Treetop to parse an ERB file. I need to be able to handle lines like the following:

ASP.Net GridView not displaying page numbers

c# asp.net gridview paging

I've gone through several articles and tutorials, but I just can't figure this out. Everything basically says, "oh just turn on AllowPaging, and you're done!" When I do that, yes, I can see the p...