1

I've searched around for a solution to my problem but couldn't find one so here it is.

I have a arraylist of data that i display using a combination of textview and checkbox. This is done using custom adapter which basically inflates it row by row. Everything up to this point is fine until i want to retrieve the checked data.

What i read from other people is that their checked boxes becomes unchecked when scrolled due to the recycling of the view. For me the state of the checkboxes remain but i don't know how to retireve them without scrolling it. When i check a box it is "check" on the UI but actaully it is not recognized until i scroll the checked item out of view and back in again.

Currently i use a global arraylist to populate with list.get(position).getName() if the list.get(position).isSelected() == true. But you see this arraylist remains empty unless i scroll the item out of view and back again. the same applies when the boxes are unchecked, they remain in the arraylist until i scroll. What i have in mind is a submit button that takes all the checkbox's current state when it is clicked then construct the arraylist.

any idea how to create such thing?

public View getView(int position, View convertView, ViewGroup parent) {
    View view = null;
    if (convertView == null) {
        LayoutInflater inflator = context.getLayoutInflater();
        view = inflator.inflate(R.layout.qlist, null);

        final ViewHolder viewHolder = new ViewHolder();
        viewHolder.text = (TextView) view.findViewById(R.id.label);
        viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
        viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {


            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Model element = (Model) viewHolder.checkbox.getTag();
                //
                if(isChecked){
                    selctionCount++;
                }
                else if (!isChecked){
                    selctionCount--;
                }
                if(selctionCount > 2)
                {
                     Toast.makeText(context, "error, you checked more than 2!", Toast.LENGTH_LONG).show();
                     element.setSelected(false);
                }
                else
                {
                    element.setSelected(buttonView.isChecked());
                }
                System.out.println(selctionCount);
            }
        });
        view.setTag(viewHolder);
        viewHolder.checkbox.setTag(list.get(position));

    } 
    else {
        view = convertView;
        ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
    }
    ViewHolder holder = (ViewHolder) view.getTag();
    holder.text.setText(list.get(position).getName());

    //This is the problem here! the selected arraylist (global) is empty even when i check the boxes, 
    //it only populates when i scroll the checked boxes out of view and back in! helphelp!
    holder.checkbox.setChecked(list.get(position).isSelected());
    if (list.get(position).isSelected() == true){
        selected.add(list.get(position).getName());
    }
    else if (list.get(position).isSelected() == false){
        selected.remove(list.get(position).getName());
    }
    return view;
}

}

ariken929
  • 67
  • 2
  • 8
  • 1
    [check this thread](http://stackoverflow.com/questions/8292189/notifydatasetchanged-for-multiple-checkboxes) – Lalit Poptani Apr 17 '12 at 11:15
  • Hi, thx for the link but my problem is to get the state of all the checkboxes when i click the button whether check or unchecked, not a button to check every box. am i missing something? – ariken929 Apr 17 '12 at 13:19
  • I've added my code to the question. – ariken929 Apr 17 '12 at 13:28

1 Answers1

1

I have already messed up with the similar problem. You can initially call a function in your constructor which initialize the states of your checkboxes as false and save all states in your arraylist. Whenever checkbox will be clicked just check the box and save its state in boolean arraylist. Later you can retrieve your states from arraylist. If you havn't understand then let me know I will try to send some lines of code to help you...

Here is a little code for your help: I will create a boolean arraylist.

private ArrayList<Boolean> itemChecked      = null;

Then I will set states of all checkboxes as false in my constructor:

    for (int i=0; i < no_of_elements.size(); i++) {
        itemChecked.add(i, false);
    }

Set the actual checked state when checkbox clicked:

        holder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                itemChecked.set(position, isChecked);

            }
        });
Bharat Sharma
  • 3,926
  • 2
  • 17
  • 29
  • im not quite sure what you mean, you said "Whenever checkbox will be clicked just check the box and save its state in boolean arraylist" can you tell me how exactly do you save its state? because the way im doing it doesnt work unless i let it recycle. – ariken929 Apr 17 '12 at 13:30
  • I have posted some lines of code. Try it. It should work for your problem. – Bharat Sharma Apr 18 '12 at 03:46
  • thx ur answer was helpful however i did fix the problem by just adding to the selected arraylist inside the listener rather than outside of with selected.add(element.getName()); However another problem came up [link](http://stackoverflow.com/questions/5925119/every-seventh-box-checked-with-checkboxes-in-listview) – ariken929 Apr 20 '12 at 15:07
  • so i just ended up not using a custom adapter and just use android's simple_list_item_multiple_choice with setChoiceMode as CHOICE_MODE_MULTIPLE – ariken929 Apr 20 '12 at 15:14
  • I do believe that [THIS](http://stackoverflow.com/questions/5925119/every-seventh-box-checked-with-checkboxes-in-listview) would be a problem if i were to display more than just a textbox with checkbox per row cause simple_list_item_multiple_choice wouldnt cut it – ariken929 Apr 20 '12 at 15:16
  • whenever we need to display complex layouts or multiple view in those cases we need to create custom adapters. If you can specify what exactly your problem is then I will try to help you I am not able to understand what problem you are exactly facing. – Bharat Sharma Apr 20 '12 at 17:26
  • @BharatSharma can u pls help me i m working on custom array adapter but i m failed to show background color on all selected listview item – user3233280 Nov 20 '14 at 16:24
  • @user3233280 Solution is similar to above. Just create an arraylist as color code with index same as row number. Check the row number in getView method and apply row color according to array list index. Dont foget to set default color in your arraylist. – Bharat Sharma Nov 23 '14 at 12:24