0

I need to get a specific ImageView inside my GridView. I tried this, but it give a NullPointerException:

ImageView imageView = (ImageView) myGridView.getChildAt(1);
imageView.setSomething();

Can someone tell me how to fix this? Thank you

hyperloris
  • 346
  • 1
  • 7
  • 14

3 Answers3

3

I assume that you are doing this in the creation phase (onCreate(), onResume(), etc), but the Views are not drawn until after all of these "creation" methods have finished. Sadly there is no callback onFinishedDraw()...

However you can put your code in a Runnable:

Runnable tweakChild = new Runnable() {
    @Override
    public void run() {
        ImageView imageView = (ImageView) myGridView.getChildAt(1);
        imageView.setSomething();
    }
};

And pass this Runnable to your GridView's Handler (inside onCreate() for example):

myGridView.post(tweakChild);
Sam
  • 86,580
  • 20
  • 181
  • 179
1

You need to simply implement the getItem() method in your ImagaViewAdapter. You must be returning null.

0

Actually, the classical way of adjusting those views is via the adapter's getView().

See some explanations here:

https://stackoverflow.com/a/6243248/1665128

How to find element inside a gridview in Android?

http://androidforums.com/developer-101/146035-get-element-view-gridview.html

Community
  • 1
  • 1
full.stack.ex
  • 1,747
  • 2
  • 11
  • 13