Search code examples
javaandroidonclickadapterinvisible

How to use setVisibility on integer array in adapter list view by onclick on ImageView in android


I'm trying to invisible an image when it is clicked by storing images from drawable into integer array using adapter list view, but I'm unable to get it. This is the code I'm using:

When i click on an image it should get invisible.I am storing images in int array and applying setVisibilty invisible but its not working.i want an image to be displayed in centre of screen and the one which is clicked should get invisible.i am trying to store images in an integer array and setting it up in adapter list.i am calling this function

imageIDs[position].setVisible(false);

    Integer[] imageIDs = {
                R.drawable.c2,
                R.drawable.c3,
                R.drawable.c4,
                R.drawable.c5,
                R.drawable.c6,
                R.drawable.c7,
                R.drawable.c8
        };
     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            // Note that Gallery view is deprecated in Android 4.1---
            Gallery gallery = (Gallery) findViewById(R.id.gallery1);
            //Adapter list
            gallery.setAdapter(new ImageAdapter(this));
            gallery.setOnItemClickListener(new OnItemClickListener() {
            //onclick event              
  public void onItemClick(AdapterView<?> parent, View v, int position,long id)
                {//displaying image clicked i am trying to invisible this pic when click
                    Toast.makeText(getBaseContext(),"pic" + (position + 1) + " selected",//dispplpaying msg
                            Toast.LENGTH_SHORT).show();
                    //imageIDs[position].setVisible(false);
                    // display the images selected
                    ImageView imageView = (ImageView) findViewById(R.id.image1);
                    imageView.setImageResource(imageIDs[position]);
                    //setting image on screen from using xml 
                }
            });
        }
        public class ImageAdapter extends BaseAdapter {
            private Context context;
            private int itemBackground;
            public ImageAdapter(Context c)
            {
                context = c;
                // sets a grey background; wraps around the images
                TypedArray a =obtainStyledAttributes(R.styleable.MyGallery);
                itemBackground = a.getResourceId(R.styleable.MyGallery_android_galleryItemBackground, 0);
                a.recycle();
            }
            // returns the number of images
            public int getCount() {
                return imageIDs.length;
            }
            // returns the ID of an item
            public Object getItem(int position) {
                return position;
            }
            // returns the ID of an item
            public long getItemId(int position) {
                return position;
            }
            // returns an ImageView view
            public View getView(int position, View convertView, ViewGroup parent) {
                ImageView imageView = new ImageView(context);
                //imageIDs[position].setVisible(false);
                //i am trying it here but its not working
                imageView.setImageResource(imageIDs[position]);
                imageView.setLayoutParams(new Gallery.LayoutParams(100, 100));
                imageView.setBackgroundResource(itemBackground);
                return imageView;
            }
        }
    }

enter image description here


Solution

  • I'm assuming you're trying to use this code:

    //imageIDs[position].setVisible(false);
    

    If so then what you're doing is calling setVisible on an Integer, which does not have that method. What you need to do is get a reference to the ImageView in which the image is being displayed and then call setVisibility(View.INVISIBLE) or setVisibility(View.GONE) on it.

    Also it seems like you're trying to set the image to invisible but then you go and put the same resource back into the ImageView so I'm not sure what you're trying to do there.