0

When i click on the images it doesnt change the display textview, where did i go wrong?

    **List<ImageView> fields = new ArrayList<ImageView>();
    for(int i = 0; i < 64; i++) {
        try{
            id = R.id.class.getField("square" + (i+1)).getInt(0);
            views.add((ImageView)findViewById(id));
            ((View) fields).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View fields) {
                    for(int i=0; i < 64; i++)
                    {
                        display = (TextView) findViewById(R.id.textView1);
                        display.setText("square" + i);
                    }
                }
            });
        }
        catch(Exception e){}

    }**

}

}

5
  • what does it display? square63? Commented Oct 20, 2013 at 16:32
  • use display.append(" \n square" + i); to check textview text is updating or not Commented Oct 20, 2013 at 16:35
  • it should display the square clicked. it's a chess game where i got 64squares. example if i click the first square it should show square1 and the last square63 Commented Oct 20, 2013 at 16:39
  • as zymurgeek has mentioned , you are doing wrong operation on list. By catching exception you are suppressing the error that you could have easily identified. So donot catch generic exception Commented Oct 20, 2013 at 17:51
  • Managed to solve it Zymurgeeks answer worked i was just displaying it wrong so i didnt see that it actually worked. So thx alot! Commented Oct 20, 2013 at 18:42

1 Answer 1

1

It looks like you're adding the onClick listener to the ArrayList of views, not the image you added. Change

 ((View) fields).setOnClickListener

to

 findViewById(id).setOnClickListener
Sign up to request clarification or add additional context in comments.

2 Comments

Hi thx for the help but after i did what you said it is clickable but it only shows square63 on every square i click. what i want the OnClickListener to do is show square1 if i click square1 and so on.
There's another issue in the code. For each click, it iterates through 0-63 setting the text for the clicked image 64 times. So you only see the last set which is "square 63". In your loop, you need to compare the fields passed to onClick to the items in your ArrayList to see which image was clicked, then only issue a setText for the found item.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.