3
  • What i am doing:: I have a gridview where i am displaying images I adapter of the gridview has image and a checkbox
  • What is happening:: When i check the checkbox and i scroll the gridview down & when i scrollback, the selected checkbox is unselected
  • Question:: How can i prevent this What changes should i need to make in the code

grid_view_image_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/flag"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_alignParentRight="true"
        android:scaleType="centerCrop"
        android:background="#000000"
        android:padding="1dp" />

    <CheckBox
        android:id="@+id/ch_bx"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="CheckBox" />

</RelativeLayout>

AdapterGridViewImage.java

public class AdapterGridViewImage extends BaseAdapter {

    // Declare Variables
    Context context;
    LayoutInflater inflater;
    ArrayList<HashMap<String, String>> data;
    HashMap<String, String> resultp = new HashMap<String, String>();

    public AdapterGridViewImage(Context context,
            ArrayList<HashMap<String, String>> arraylist) {
        this.context = context;
        data = arraylist;
        //imageLoader = new ImageLoader(context);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        ImageView flag;
        CheckBox ch_bx;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.grid_view_image_item, parent, false);
        resultp = data.get(position);
        flag = (ImageView) itemView.findViewById(R.id.flag);
        ch_bx=(CheckBox) itemView.findViewById(R.id.ch_bx);
        //Picasso.with(context).load("http://10.0.2.2:3009/buffet_items/".trim()+resultp.get("item_image").trim()).into(flag);

         Picasso.with(context)
          .load("http://10.0.2.2:3009/buffet_items/".trim()+resultp.get("item_image").trim())
          .resizeDimen(R.dimen.wanted_size,R.dimen.wanted_size).centerCrop().into(flag);

        return itemView;
    }
}
Devrath
  • 42,072
  • 54
  • 195
  • 297
  • 1
    Take a look at this question: http://stackoverflow.com/questions/4718717/refreshing-checkbox-state-in-a-listview – LordRaydenMK Jun 03 '14 at 13:01
  • 1
    http://stackoverflow.com/questions/18162931/android-get-selected-item-using-checkbox-in-listview-when-i-click-a-button. Will work for gridview also. views are recycled. that is why it gets unchecked when you scroll – Raghunandan Jun 03 '14 at 13:02
  • just change your getview method code..use this link http://stackoverflow.com/questions/17234399/in-gridview-checkbox-is-unchecked-while-scrolling-gridview-up-and-down – Meenal Jun 03 '14 at 13:20

1 Answers1

0

Please modify your code.
1) We have to use ViewHolder class for encapsulation.
2) We have to set tag values of checkbox so when view recreate at that time we can take values from tag.

 public AdapterGridViewImage(Context context,
                        ArrayList<HashMap<String, String>> arraylist) {
                    this.context = context;
                    data = arraylist;
                    //imageLoader = new ImageLoader(context);
                    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            }

            @Override
            public int getCount() {
                return data.size();
            }

            @Override
            public Object getItem(int position) {
                return null;
            }

            @Override
            public long getItemId(int position) {
                return 0;
            }

            public View getView(final int position, View convertView, ViewGroup parent) 
            {
                final ViewHolder holder;
                if(convertView==null)
                {
                    holder=new ViewHolder();
                    convertView = inflater.inflate(R.layout.grid_view_image_item, parent, false);
                    holder.flag = (ImageView) convertView.findViewById(R.id.flag);
                    holder.ch_bx=(CheckBox) convertView.findViewById(R.id.ch_bx);
holder.ch_bx.setTag(false);
                    convertView.setTag(holder);
                }
                else
                {
                    holder=(ViewHolder)convertView.getTag();
                }

                resultp = data.get(position);

                if((Boolean)holder.ch_bx.getTag())
                {
                    holder.ch_bx.setChecked(true);
                }
                else
                {
                    holder.ch_bx.setChecked(false);
                }

                holder.ch_bx.setOnClickListener(new OnClickListener()
                {

                    @Override
                    public void onClick(View v)
                    {
                        if(holder.ch_bx.isChecked())
                        {
                            holder.ch_bx.setTag(true);
                        }
                        else
                        {
                            holder.ch_bx.setTag(false);
                        }
                    }
                });
                //Picasso.with(context).load("http://10.0.2.2:3009/buffet_items/".trim()+resultp.get("item_image").trim()).into(flag);

                 Picasso.with(context)
                  .load("http://10.0.2.2:3009/buffet_items/".trim()+resultp.get("item_image").trim())
                  .resizeDimen(R.dimen.wanted_size,R.dimen.wanted_size).centerCrop().into(holder.flag);

                return itemView;
            }
            class ViewHolder
            {
                ImageView flag;
                CheckBox ch_bx;
            }
Bhavesh Jethani
  • 3,891
  • 4
  • 24
  • 42