codingNewb
codingNewb

Reputation: 482

android preserve gridview item setChecked not working

ok, so I seem to be having some trouble getting the setChecked method of a custom RelativeLayout used for gridView to be called properly. My approach so far: I'm preserving a boolean list between rotations so that when the getView method of the gridview's base adapter is called, I can set some data depending on if the boolean value is checked or not. The idea is that I'm preserving a boolean array for when the device changes orientation. However no matter what I try, I can't seem to get my getView method to actually properly use setChecked. Here is my code so far:

the custom RelativeLayout:

    public class CustomRelativeLayoutForGridView extends RelativeLayout implements
            Checkable {

        private boolean checked = false;
        private static final int[] CHECKED_STATE_SET = { android.R.attr.state_checked };

        public CustomRelativeLayoutForGridView(Context context, AttributeSet attrs,
                int defStyle) {
            super(context, attrs, defStyle);
        }

        public CustomRelativeLayoutForGridView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        public CustomRelativeLayoutForGridView(Context context) {
            super(context);
        }

        @Override
        protected int[] onCreateDrawableState(int extraSpace) {
            final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
            if (isChecked())
                mergeDrawableStates(drawableState, CHECKED_STATE_SET);
            return drawableState;
        }

        @Override
        public boolean isChecked() {
            return checked;
        }

        @Override
        public void setChecked(boolean _checked) {
            checked = _checked;
            setBackgroundColor(checked ? 0xFF00FF00 : 0x00000000);
            refreshDrawableState();
        }

        @Override
        public void toggle() {
            setChecked(!checked);
        }
    }

the holder:

    class myViewHolder {
        TextView text;
        ImageView icon;
        CustomRelativeLayoutForGridView rLayout;
        myViewHolder(View v) {
            text = (TextView) v.findViewById(R.id.grid_item_label);
            icon = (ImageView) v.findViewById(R.id.grid_item_image);
            rLayout = (CustomRelativeLayoutForGridView) v.findViewById(R.id.gridViewCellLayout);
        }
    }

the getView:

    public View getView(final int position, View convertView, ViewGroup parent) {
            View gridView = convertView;
            myViewHolder holder = null;
            if (gridView == null) {
                gridView = new View(context);
                // get layout from mobile.xml
                LayoutInflater inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                gridView = inflater.inflate(R.layout.file_manager_display_item,
                        null);
                holder = new myViewHolder(gridView);
                gridView.setTag(holder);
            } else {
                holder = (myViewHolder) convertView.getTag();
            }
            holder.text.setText(direcsPics[position].getName());
            if(imageSelected[position]) {
                holder.rLayout.setChecked(true);
            } else {
                holder.rLayout.setChecked(false);
            }
            return gridView;
        }

as you can see in the getView method I'm definetly calling the setChecked method, but nothing seems to happen. I've double checked that the boolean values are being preserved properly, but setChecked doesn't seem to do anything at all. Does anyone out there hopefully have an idea what's going on and how to fix this? I'd sure appreciate any help I can get ^_^ I've been working on this code for a week now with no luck =(

Upvotes: 1

Views: 1014

Answers (1)

codingNewb
codingNewb

Reputation: 482

Ok! Finally figured it out I think =) Was a real brain-teaser for me. First I'll look at the part that was really confusing to me:

if(imageSelected[position]) {
    holder.rLayout.setChecked(true);
} else {
    holder.rLayout.setChecked(false);
}

Notice my setChecked calls. This is where I was confused, I mean the code from the custom RelativeLayout was assuredly changing the backgrounds colors, since after removing the setBackgroundColor line my highlighting no longer worked. Well, I threw some Log calls to the setChecked method of the RelativeLayout and some here where I was calling setChecked, but I noticed a lot more calls to setChecked than I was doing, and all those called after mine. So android's recalling the views. Well, then I recalled how my gridview is setup in the xml:

<GridView
    android:id="@+id/gridView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/fileManagerAcceptButton"
    android:columnWidth="100dp"
    android:drawSelectorOnTop="false"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:gravity="center"
    android:horizontalSpacing="5dp"
    android:choiceMode="multipleChoice"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:verticalSpacing="5dp" >
</GridView>

notice the choice mode? So, if something else is taking preference over the setChecked calls, it's probably this. So maybe I need to tell the gridview parent what's checked and not try to specify it in the child. After this I went back to my activity and added this to my OnCreateView(it's a dialog fragment):

for (int i = 0; i < gridView.getCount(); i++) {
    if (transferBoolArray[i]) {
        gridView.setItemChecked(i, true);
    } else {
        gridView.setItemChecked(i, false);
    }
}

problem solved for me =) I just hope my answer helps someone in the future ^_^

Upvotes: 2

Related Questions