2

I'm looking to implement a list view with multiple check boxes. The user should be able to select multiple check boxes and click on 'submit' button to proceed further.

I'm using a custom adapter to display my own list view and override getView() method.

So far so good, I'm able to display the list with check boxes and I can select multiple check boxes, but when I'm trying to get the selected list item details (using the list.get(position).getName()), it is started bouncing. I mean, when I start scrolling the list view to select further items, the value of the above is changing to different items and not keeping the original.

My code is here:

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.checkboxlayout, 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();
    element.setSelected(buttonView.isChecked());
//Toast.makeText(getContext(), "You just checked: "+list.get(position).getName(), Toast.LENGTH_SHORT).show();
    System.out.println("The element name is: "+element.getName());
    System.out.println("Name of the checked item is--outside loop:"+list.get(position).getName());

}

});

Is there a way to constrain or hold the value of the items I selected even after keep scrolling to check for other items.

bharath
  • 14,283
  • 16
  • 57
  • 95
kittu
  • 67
  • 2
  • 11
  • Checkout my answer [here](http://stackoverflow.com/questions/7738527/getting-an-issue-while-checking-the-dynamically-generated-checkbox-through-list/7738854#7738854) and also the complete thread to get exactly what is happening here. Knickedi has explained awesomely below my answer. – Lalit Poptani Nov 22 '11 at 08:18
  • Hi Lalit, Thanks for the response. I have gone through both your original solution and the Knickedi's approach. Although your solution is working for me, as you suggested to get along with Knickedi's approach, I'm trying to implement what he suggested(though not a straight solution). I'll try how best I can implement his suggestion. – kittu Nov 23 '11 at 07:25
  • Well the approach I answered is the correct one there now. – Lalit Poptani Nov 23 '11 at 07:26

2 Answers2

1

The problem is that you are recycling the convertView provided by the system. If it is present (it is not null) this does not mean that it contains the exact old view you'd expect but maybe another one.

So it may happen that the system is recycling a "ticked" checkbox view where the old checkbox was unticked and viceversa. I had this situation so focusing on this issue might help you solve the problem you're facing.

EDIT: to solve this issue you simply have to reinitialize view attributes if you have a recycled view or, (discouraged) simply ignore the convertView parameter and create a new view every time the getView method is called

STT LCU
  • 4,348
  • 4
  • 29
  • 47
  • Yes STT LCU (Sorry can't get your short name though),thanks for the response and yeah, you are right. I'm too trying to understand how to avoid such a situation where recycling happens. – kittu Nov 23 '11 at 07:28
  • i have edited my answer with a small solution suggestion. please tick the answer as accepted if you think that it is the right one. – STT LCU Nov 23 '11 at 07:36
0

You don't have to write a custom adapter or a custom view. Android has already implemented the functionality you need, and using it may just bypass your problem entirely.

All you have to do is set the 'choice mode' of your ListView to CHOICE_MODE_MULTIPLE: setChoiceMode(int)

You can retrieve the checked items with methods like getCheckedItemCount() getCheckedItemIds(). More information here: android:choiceMode.

mhelvens
  • 4,225
  • 4
  • 31
  • 55