Search code examples
javaandroidgridviewadapter

How to change multiple image at once in gridview in Android?


I'm working on AndroidStudio with Java.

I have many imageviews inside gridview. and I'm trying to apply click event which affects multiple imageviews at the same time

what I want to do is : when one imageview is clicked another imageview, which is not been clicked to change its image.

for example, there is two imageview A and B in same gridview. if I click A imageview, both A and B imageview set to different imagesources.

what I can do is change only clicked imageview. I want to know how to access the unclicked items in gridview. I made onclick listener inside adapter.

public View getView(int position, View convertView, ViewGroup parent) {
        convertView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.ingrid,parent,false);

            ImageView blackorwhite = convertView.findViewById(R.id.blackOrWhite);

            Integer val = mData.get(position);
            blackorwhite.setImageResource(blockColor.get(val));
            blackorwhite.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    blackorwhite.setImageResource(R.color.white);
                }
            });

Solution

  • you can make an interface callback to know which item gets clicked for example this is ur interface

    interface OnClickListener {
        void onClick(int position)
    }
    

    and pass this interface to ur Adapter, and in the getView method whenever a view get clicked u can call the onClick method of the interface

    this is example code

    interface OnClickListener {
        void onClick(int position);
    }
    
    public class GridViewAdapter extends BaseAdapter {
    
    
        private List<String> list;
        private OnClickListener listener;
    
        private Context context;
    
        public GridViewAdapter(Context context,List<String> list,OnClickListener listener){
            this.listener = listener;
            this.context = context;
            this.list = list;
        }
    
    
        @Override
        public int getCount() {
            return list.size();
        }
    
        @Override
        public Object getItem(int position) {
            return list.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
           View  view = LayoutInflater.from(context).inflate(ur layout);
    
    
           view.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   listener.onClick(position);
               }
           });
    
            return view;
        }
    }
    
    

    To initiate The adapter, u can use this code,

     GridViewAdapter adapter = new GridViewAdapter(context, imageList, new OnClickListener() {
                @Override
                public void onClick(int position) {
                    // this method called every time an view get clicked
                    // and u can change the DataSet which now is imageList
                   // imageList.set(position,"something new ");
                    // after the change of DataSet u should cal the notifyDataSetChanged
                    adapter.notifyDataSetChanged();
                }
            });