0

I have the adapter code below which is backed by a multidimensional array. The code is inside a ListFragment which has a parent Activity.

when a checkbox is selected, I call a listener which sends information to the parent Activity.

All this works fine. My problem is, how can I make a button select/deselect all the checkboxes in the list and send the information in each row to the listener that will send the info to the parent Activity?

I have found the following code that selects all the checkboxed but this doesn't take into account the data in each row that needs to go to the parent Activity.

final ListView list = getListView();
    for ( int i=0; i< getListAdapter().getCount(); i++ ) {
            list.setItemChecked(i, true);
    }
private class MySimpleArrayAdapter extends ArrayAdapter<String> {
        private final Context context;
        private final ArrayList<?> list;

        boolean[] checkBoxState;
        ViewHolder viewHolder;
        public MySimpleArrayAdapter(Context context, ArrayList<?> list) {

            super(context, R.layout.rotarowlayout);
            Log.e(TAG, "inside adapter constructor");
            this.context = context;
            this.list = list;

            checkBoxState = new boolean[this.list.size()];
        //Log.e(TAG, "list has size of " + this.list.size());
        }

        private class ViewHolder
         {

            TextView startTime;
            TextView duration;
            TextView status;
            TextView name;
            TextView actualIn;
            TextView actualOut;
            TextView carerName;
            TextView statusBox;
            ImageView noteStatus;
            ImageView doubleupStatus;
            ImageView timeCritical;

            CheckBox checkBox;
         }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

             if(convertView == null)
                {
                        convertView = inflater.inflate(R.layout.rotarowlayout, null);
                        viewHolder = new ViewHolder();

                        viewHolder.startTime = (TextView) convertView.findViewById(R.id.rowstarttime);
                        viewHolder.duration = (TextView) convertView.findViewById(R.id.rowduration);
                        viewHolder.status = (TextView) convertView.findViewById(R.id.rowstatus);
                        viewHolder.name = (TextView) convertView.findViewById(R.id.rowclientname);
                        viewHolder.noteStatus = (ImageView) convertView.findViewById(R.id.notestatus);
                        viewHolder.doubleupStatus = (ImageView) convertView.findViewById(R.id.doubleupstatus);
                        viewHolder.timeCritical = (ImageView) convertView.findViewById(R.id.timecritical);
                        viewHolder.actualIn = (TextView) convertView.findViewById(R.id.rowclientactualin);
                        viewHolder.actualOut = (TextView) convertView.findViewById(R.id.rowclientactualout);
                        viewHolder.carerName = (TextView) convertView.findViewById(R.id.rowruncarername);
                        viewHolder.statusBox = (TextView) convertView.findViewById(R.id.rowstatus);

                        viewHolder.checkBox=(CheckBox) convertView.findViewById(R.id.list_checkbox_multi_select_carer);

                         //link the cached views to the convertview
                        convertView.setTag( viewHolder);
                }else
                {             
                   viewHolder = (ViewHolder) convertView.getTag();
               }
            String record = list.get(position).toString();
            convertView.setTag(R.layout.rotarowlayout, record);
            String[] itemsInRecord = record.split(",");
            Log.e(TAG, "itemin record = " + itemsInRecord.length);
            final String[] recordItem = new String[itemsInRecord.length];

            for (int x = 0; x < itemsInRecord.length; x++) {
                recordItem[x] = itemsInRecord[x];
                Log.e(TAG, "recordItem[x]" + x + " " +recordItem[x] );
            }
            viewHolder.checkBox.setChecked(checkBoxState[position]);
             viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                 if(((CheckBox)v).isChecked()){
                    checkBoxState[position] = true;
                   mListener.onMultiSelectUpdateCallSelected("ADD", recordItem[5]);
}else{
 checkBoxState[position] = false;
 mListener.onMultiSelectUpdateCallSelected("REMOVE", recordItem[5]);
}
  }

});
    return convertView;
}
@Override
public int getCount() {
if(this.list != null){
return this.list.size();
}else{
 return 0;
 }}
  }// end of adapter class
abarisone
  • 3,707
  • 11
  • 35
  • 54
turtleboy
  • 8,210
  • 27
  • 100
  • 199
  • Have a look on it : http://stackoverflow.com/questions/4553186/android-checkbox-listview-select-all-disable-enable – KishuDroid Jul 25 '16 at 10:29
  • @KishuDroid Hi that is the same post i got my code from. unfortunately the accepted answer only works with cursorAdapter. i'm using ArrayAdapter. – turtleboy Jul 25 '16 at 10:40
  • You do not send information to each listener. You only set the wanted values in your multidimensional array. After that call NotifyDataSetChanged on the adapter. Why are you calling `boolean[] checkBoxState;` multidimensional? You only have to set this array and you are done. – greenapps Jul 25 '16 at 11:27
  • @turtleboy: Please check this link with ArrayAdapter : http://stackoverflow.com/questions/10911361/how-to-get-selected-list-items-from-a-listview-with-checkbox-and-custom-adapter – KishuDroid Jul 25 '16 at 12:48
  • @greenapps private final ArrayList> list uses the following object to populate it. TwoDimentionalArrayList array; I send the following to the listener. recordItem[5] is a String GUID. mListener.onMultiSelectUpdateCallSelected("ADD", recordItem[5]); – turtleboy Jul 25 '16 at 14:43

0 Answers0