Skip to content Skip to sidebar Skip to footer

Saving The List Of Checkboxes Of Listview Built Using A Custom Adapter

Now this is something that has been bothering me for a long time now. But somehow I still am not able to figure out how to save the state of checkboxes in a listview built using a

Solution 1:

You can try this:

holder.cb.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener() {

        @OverridepublicvoidonCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub

             itemChecked[position] = isChecked;
             if(itemChecked[position])
             {
                 holder.cb.setChecked(true);
             }
             else
             {
                 holder.cb.setChecked(false);
             }

             boolean sub=isChecked;

             ***save(itemChecked);***
       }
});

Solution 2:

Try this, Instead of using OnCheckedChangeListener, just used OnClickListener :

@Overridepublic View getView(finalint position, View convertView, ViewGroup arg2) {
        // TODO Auto-generated method stubfinal ViewHolder holder;  
        LayoutInflaterinflater=  ((Activity) context).getLayoutInflater();  
        if(convertView==null)  
        {  
            convertView = inflater.inflate(R.layout.custom_list, null);
            holder = newViewHolder();  

             holder.txtViewTitle = (TextView) convertView.findViewById(R.id.title_text);  

             holder.txtViewDescription = (TextView) convertView.findViewById(R.id.description_text);

            holder.cb=(CheckBox) convertView.findViewById(R.id.cb);

            convertView.setTag(holder);  

        }  

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

         }  

        holder.txtViewTitle.setTextColor(Color.parseColor("#008ab5"));
        holder.txtViewTitle..setText(title[position]);
        holder.txtViewDescription.setTextColor(Color.parseColor("#008ab5"));
        holder.txtViewDescription.setText(description[position]);
        if (itemChecked[position])
              holder.cb.setChecked(true);
         else
              holder.cb.setChecked(false);

        holder.cb.setOnClickListener(newOnClickListener() {
                @OverridepublicvoidonClick(View v) {
                    // TODO Auto-generated method stubif (holder.cb.isChecked())
                        itemChecked[position] = true;
                     else
                         itemChecked[position] = false;
                }        
        });
        return convertView;

    }  

Post a Comment for "Saving The List Of Checkboxes Of Listview Built Using A Custom Adapter"