Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Displaying Checkmarks in Gridview challenge objectives 2 and 3

Now, add an if statement to check if an item at position in mGridView is checked. The items get set as "checked" when the player is online. If it is checked, make sure that holder.overlayImageView has its visibility set to View.INVISIBLE.

Add an if statement where? Under the line that defines "holder.overlayImageView":

holder.overlayImageView  = (ImageView)convertView.findViewById(R.id.overlayImageView);
    if (mGridView.isItemChecked()){

    }   

Adding my attempt at an if statement in the part of the code just gave me a parse error!

What if I just add the if statement under the line from the first part of the challenge:

    mGridView = (GridView) parent;
    if (mGridView.isItemChecked()){

    }   

That's still give me an error.

What does "if an item at position" mean?

Does it mean I'm suppose to put the word 'position'

somewhere in the if statement?

The only place I could think to put it is:

    mGridView = (GridView) parent;
    if (mGridView.isItemChecked(position)){

    }

That doesn't give me an error, just a Bummer!

So something else has got to be there...hmm..

It said something about:

"holder.overlayImageView has its visibility set to View.INVISIBLE"

What does that mean?

I tried:

    if (mGridView.isItemChecked(position)){
        holder.overlayImageView(View.INVISIBLE);
    }

But it doesn't like that.

After over an hour of research I found these forum posts:

https://teamtreehouse.com/forum/updating-other-gridviews-code-challenge-step-4-of-5

https://teamtreehouse.com/forum/gridview-check-image-are-not-added-when-grid-is-tapped

https://teamtreehouse.com/forum/display-an-image-on-a-listview

that suggested using:

.setVisibility(View.INVISIBLE);

So that means the code is probably:

    if (mGridView.isItemChecked(position)){
        holder.overlayImageView.setVisibility(View.INVISIBLE);
    }

However I still had trouble with the final part of the challenge using:

         mGridView = (GridView) parent;
    if (mGridView.isItemChecked(position)){
        holder.overlayImageView.setVisibility(View.INVISIBLE);
        else 
        holder.overlayImageView.setVisibility(View.VISIBLE);
    }

Not passing!

After many many random attempts:

public class PlayerAdapter extends ArrayAdapter<ParseUser> {

    public GridView mGridView;

    /*
     * Some code has been omitted for brevity!
     */

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.player_item, null);
            holder = new ViewHolder();
            holder.playerImageView = (ImageView)convertView.findViewById(R.id.playerImageView);
            holder.overlayImageView  = (ImageView)convertView.findViewById(R.id.overlayImageView);
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder)convertView.getTag();
        }

        /*
         * Add your code here! (Some code has been omitted for brevity)
         */



        //this is what they wanted.. 
         mGridView = (GridView) parent;
    if (mGridView.isItemChecked(position)){
        holder.overlayImageView.setVisibility(View.INVISIBLE);
    }    else {
        holder.overlayImageView.setVisibility(View.VISIBLE);
    }




        return convertView;
    }

    private static class ViewHolder {
        ImageView playerImageView;
        ImageView overlayImageView;
    }
}

Whew! Only one more section to go

..I've spent all day on this (not even counting the time spent watching the videos..)

1 Answer

Ben Wong
Ben Wong
19,426 Points

mGridView = (GridView)parent; if(mGridView.isItemChecked(position)){ holder.overlayImageView.setVisibility(View.INVISIBLE); } else{ holder.overlayImageView.setVisibility(View.VISIBLE); }