0

I am facing issue in accessing individual item inside a gridview. I have a grid view of images. On Long press of individual item I am activating CAB menu with multi mode listener. Inside the listener, I am able to do

gridView.setVisibility(View.GONE);

But I am unable to do this and it gives NUll Pointer Exception

gridView.getChildAt(position).setVisibility(View.GONE);

I have tried this also:

ViewGroup gridChild = (ViewGroup) gridView.getChildAt(1);
int childSize = gridChild.getChildCount();
for(int k = 0; k < childSize; k++) {
 if( gridChild.getChildAt(k) instanceof ImageView ) {
      gridChild.getChildAt(k).setVisibility(View.GONE);
 }
}

How can I overcome this?

Update:

Inside my Adapter getView() method:

 imageview.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
    overlay.setVisibility(View.VISIBLE);
    imageview.setBackgroundColor(Color.parseColor("#f9a49c"));
    return false;
}});

What I am trying to do here is, I just want to change the background color onclick. But the color is changing for the last element in the current view. And also that too not staying after press. It just goes off after press.

intrepidkarthi
  • 3,104
  • 8
  • 42
  • 75

2 Answers2

1

In your adapter's getView() method, you should set the view to visible or not, depending on the state the data backing the adapter.

You shouldn't assume that every item has a unique view, adapters typically reuse the views of items that go out of the screen when scrolling.

Bastien Léonard
  • 60,478
  • 20
  • 78
  • 95
  • Updated the question. What I am trying to do here is, I just want to change the background color onclick. But the color is changing for the last element in the current view. And also that too not staying after press. It just goes off after press. – intrepidkarthi Feb 05 '13 at 17:07
0

You can try to notify adapter

if( gridChild.getChildAt(k) instanceof ImageView ) {

gridChild.getChildAt(k).setVisibility(View.GONE);

notifyDataSetChanged();
}
Bobans
  • 439
  • 6
  • 13