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

David Gómez
David Gómez
4,389 Points

How to express position of item in GridView

Hi everybody, I'm trying to build an app (a bingo) and I want to create an if statement indicating when the cells have been clicked. For example, if positions 4, 5, 6 and 7 have been clicked, it will display a toast. I'm giving those cells a different color when clicked. My problem is that I haven't been able to find how to express the position of those cells, to say for instance "if (position[4].equals(Color.WHITE) && position[4].equals(Color.WHITE)... )".

Of course "[]" is not how I should express it, but after days searching I haven't been able to find how. I'd appreciate if somebody could give me some information.

3 Answers

Are you using indexes correctly?

Here's a tip:

Humans start counting at one, so they count like this: 1, 2, 3, 4, 5 Computers start counting at zero, so they count like this (unlike humans): 0, 1, 2, 3, 4

So, let's say we have a list of random thing including apple, bumblebee, and cactus. For a human, bumblebee is at index 2, but for a computer, it is one less than that (in other words, bumblebee will be at 1 to a computer). Hope this might help! :)

David Gómez
David Gómez
4,389 Points

Well, this is embarrasing but I wasn't using any (first time I try to do this). I searched information and now I have an idea, I don't have my computer at the moment but I'll try later and I'm telling you if it works. Thanks!

No problem I'm always happy to help :)

David Gómez
David Gómez
4,389 Points

Ready. I looked for that info and tried to use it but I couldn't figure out how to apply it. I paste here what I tried but it's not working, it would be great if you had any suggestion :D

public class MyActivity extends Activity {

    String[] choices = new String[12];

    {
        choices[0] = "A";
        choices[1] = "B";
        choices[2] = "C";
        choices[3] = "D";
        choices[4] = "E";
        choices[5] = "F";
        choices[6] = "G";
        choices[7] = "H";
        choices[8] = "I";
        choices[9] = "J";
        choices[10] = "K";
        choices[11] = "L";

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        final GridView gridview = (GridView) findViewById(R.id.gridview);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, choices);

        gridview.setAdapter(adapter);

        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                                    int position, long id) {

                v.setBackgroundColor(Color.WHITE);
                if (v.getTag(position).equals(5) && v.getTag(position).equals(6) && v.getTag(position).equals(5)) {
                    Toast.makeText(getApplicationContext(), "Line!", Toast.LENGTH_LONG).show();
                }


            };

        });


        }

    } 

lol I'm rather new to android I don't really understand any of that sorry XD

David Gómez
David Gómez
4,389 Points

ROFL no prob XD I think I'll have to email Ben hahaha

lol good luck