0

I have the code below, now I need to keep track of the checkbox state in each gridview item, and fetch that info on a button click to update the information. My button event in the calling activity of imageadapter for a gridview.

    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder;
        ImageView imgView = null;

            if (convertView == null) {

                holder = new ViewHolder(); 
                LayoutInflater ltInflate = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE ); 
                convertView = ltInflate.inflate(R.layout.griditem, null);

                holder.textview1 = (TextView) convertView.findViewById(R.id.grid_item_alert_date);
                holder.textview2 = (TextView) convertView.findViewById(R.id.grid_item_alert_time);
                holder.textview3 = (TextView) convertView.findViewById(R.id.grid_item_alert_type);

                holder.imageview    = (ImageView) convertView.findViewById(R.id.grid_item_image);
                holder.checkbox = (CheckBox) convertView.findViewById(R.id.checkbox_ack);
                convertView.setTag(holder);

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

            holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                  @Override
                  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
                {

                    //Accessing or saving position to a List doesn't work here

                    //How to add ? info to a list here...

                }

                  } });

            holder.textview1.setText("Text 1 ");
            holder.textview2.setText("Text 2 ");
            holder.textview3.setText("Text 3 ");
            holder.checkbox.setChecked(false);
            holder.imageview.setImageBitmap(bitmap);
            holder.id = position;

            return convertView;
    }

In the activity:

private OnClickListener UpdateButtonListener =
            new OnClickListener(){
            public void onClick(View v)
            {
            //CheckBox ckbocx = (CheckBox) findViewById(R.id.checkbox_ck);
                        //Need info on all the checkboxes for each gridview item

    };

Any clues, hints are more than welcome.

AliR
  • 2,065
  • 1
  • 27
  • 37
  • you want to updated checked value on particular button click, right? – RobinHood Oct 10 '12 at 05:13
  • @RobinHood thanks for your msg. No only one button for all the checkboxes checked. The button just gets list of the checkboxes which are active/checked and does work based on this info. – AliR Oct 10 '12 at 05:15
  • on click of button all checkboxes get checked? – RobinHood Oct 10 '12 at 05:44
  • the button needs to get the info only about all the checkboxes it doesn't interacts (checked/unchecked). So I can't seem to find a way to get info about all the checkboxes. – AliR Oct 10 '12 at 05:48
  • come here http://chat.stackoverflow.com/rooms/5098/android-people – RobinHood Oct 10 '12 at 05:54

1 Answers1

4

Deal with integer array to store the state of your checkboxes when it checked/unchecked, Initially fill the array with 0 values which indicate unchecked of your checkboxes like this.

int[] checkStates;
 checkStates = new int[datalist.length()];
   for (int i = 0; i < datalist.length(); i++) {
        checkStates[i] = 0;
   }

Now handle checkboxes click event to get perfect position. use settag & get gettag and inside click event when box get selected change the value of particular position to 1 from 0.

Like this#

  checkbox.setTag(position);



    checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            pos = (Integer) buttonView.getTag();
            // checkStates[pos] = 1;
            // pos = (Integer) v.getTag();

            if (!buttonView.isChecked()) {
                boxState[pos] = 0;
            } else {
                boxState[pos] = 1;
            }
            notifyDataSetChanged();
        }
    });

and inside getview method handle your check/uncheck state this way..

if (checkStates[position] == 0) {
            checkbox.setChecked(false); // set unchecked"
        } else {
            checkbox.setChecked(true); // set checked"
        }

This way you will get the info of checkboxes which are selected, further handle your button click event, and get the int array which filled while check and uncheck.

Community
  • 1
  • 1
RobinHood
  • 10,897
  • 4
  • 48
  • 97
  • this "checkbox.setOnClickListener(new OnClickListener() {" is not working for me it has only checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked) – AliR Oct 10 '12 at 06:22
  • ok thanks RobinHood, but how can I move this array to the parent activity – AliR Oct 10 '12 at 06:44
  • @Adil Soomro added at the chat window:"You can get it using yourAdapter.getCheckStateArray();, make sure to write this method in your Adapter and there you will return your Adapter's array from that method." – AliR Oct 10 '12 at 07:27