Will Jamieson
Will Jamieson

Reputation: 926

GridView setOnItemClickListener effects multiple items

When I click the GridView item multiple checks appear where I just want it to appear in the one. I'm pretty sure this has something to do with the recycled views, but I am not sure how to prevent it from happening. Any advice is appreciated.

 grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    final int position, long id) {
                ImageView check = (ImageView) view.findViewById(R.id.check);
                if (check.getVisibility() == View.GONE) {
                    check.setVisibility(View.VISIBLE);

                    I
                }

                else {
                    check.setVisibility(View.GONE);

                }
            }
        });

Here is the adapter

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View grid;
    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        grid = new View(mContext);
        grid = inflater.inflate(R.layout.grid_item, null);

    } else {
        grid = (View) convertView;
    }

    ImageView imageView = (ImageView) grid.findViewById(R.id.grid_image);
    final ProgressBar pb = (ProgressBar) grid.findViewById(R.id.progressbar);
    final ImageView check = (ImageView) grid.findViewById(R.id.check);

    Ion.with(mContext).load(objects.get(position).getThumnailURL())
            .progressBar(pb).withBitmap()
            .error(R.drawable.ic_launcher).intoImageView(imageView).setCallback(new FutureCallback<ImageView>() {
                   @Override
                    public void onCompleted(Exception e, ImageView file) {
                       pb.setVisibility(View.GONE);
                    }
                });


    return grid;
}

Upvotes: 0

Views: 275

Answers (2)

Riddhi Shah
Riddhi Shah

Reputation: 477

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder _holder = new ViewHolder();
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
                 convertView =  inflater.inflate(R.layout.grid_item, null);
    _holder.imageView  = (ImageView) grid.findViewById(R.id.grid_image);
        _holder.pb  = (ProgressBar) grid.findViewById(R.id.progressbar);
  _holder.check   = (ImageView) grid.findViewById(R.id.check);
    convertView.setTag(_holder);

        } 
           else {
                _holder = (ViewHolder) convertView.getTag();
            }


       //here set your values
        Ion.with(mContext).load(objects.get(position).getThumnailURL())
                .progressBar(pb).withBitmap()
                .error(R.drawable.ic_launcher).intoImageView(imageView).setCallback(new FutureCallback<ImageView>() {
                       @Override
                        public void onCompleted(Exception e, ImageView file) {
                           pb.setVisibility(View.GONE);
                        }
                    });


        return grid;
    }
class ViewHolder {

        ImageView imageView,check;
        ProgressBar pb;

    }

Try this !!

Upvotes: 0

mmlooloo
mmlooloo

Reputation: 18977

You can do it like this:

put a boolean variable in your object that is passed to your adapter(ArrayList) and control the visibility of check boxes by this variable so if onitemclick is called toggle that variable and call notifydatasetchange. In the getView set each check boxes by the value of this variable. in this way if you scroll the gridview your value of your check boxes do not get lost.

Upvotes: 1

Related Questions