vendredi 9 novembre 2018

trying to add checked by default checkboxes to my arraylist

My checkboxes are checked, by default - with, ((MatchingContact) viewHolder).check.setChecked(theContactsList.get(position).getSelected()); That part works.

The problem is I want to have all checked checkboxes added to my arraylist, checkedContactsAsArrayList, by default, also. Which isn't happening.

When I toast with Toast.makeText(context_type, "checked checkboxes are:" + theContactsList.get(position).getPhone(), Toast.LENGTH_SHORT).show(); it toasts all the numbers I want to add to checkedContactsAsArrayListso not sure why it is not working.

When I uncheck boxes and then check them again, they are added to checkedContactsAsArrayList, but I need to have the checked boxes by default added to the checkedContactsAsArrayList, initially. Thanks for any help.

Here's my code:

  checkedContactsAsArrayList  = new ArrayList();

      if (viewHolder.getItemViewType() == 1)

      {
        //in the title textbox in the row, put the corresponding name, phone number,
        //and checkbox, checked, by default
        ((MatchingContact) viewHolder).title.setText(selectPhoneContact.getName());
        ((MatchingContact) viewHolder).phone.setText(selectPhoneContact.getPhone());
        ((MatchingContact) viewHolder).check.setChecked(theContactsList.get(position).getSelected());
        ((MatchingContact) viewHolder).check.setTag(position);


        //if the number is checked, which it will be by default
          if (theContactsList.get(position).isSelected) {

            Toast.makeText(context_type, "checked checkboxes are:" + theContactsList.get(position).getPhone(), Toast.LENGTH_SHORT).show();

            //add the checked number to checkedContactsAsArrayList
            checkedContactsAsArrayList.add(theContactsList.get(position).getPhone());

          }

        //for the onClick of the check box
        ((MatchingContact) viewHolder).check.setOnClickListener(new View.OnClickListener() {

          @Override
          public void onClick(View v) {

            //***********NEED THIS TO PRESERVE CHECKBOX STATE, otherwise
            //can be lost on scroll

            //if unclicked
            //position is the row number that the clicked checkbox exists in
            if (theContactsList.get(position).getSelected()) {
              theContactsList.get(position).setSelected(false);
              Toast.makeText(context_type, theContactsList.get(position).getPhone() + " unclicked!", Toast.LENGTH_SHORT).show();

              //remove the checked number from the arraylist
              checkedContactsAsArrayList.remove(theContactsList.get(position).getPhone());

            } else {

              //if clicked
              //position is the row number that the clicked checkbox exists in
              theContactsList.get(position).setSelected(true);
              Toast.makeText(context_type, theContactsList.get(position).getPhone() + " clicked!", Toast.LENGTH_SHORT).show();

              //add the number to the arrayList
              checkedContactsAsArrayList.add(theContactsList.get(position).getPhone());

            }




Aucun commentaire:

Enregistrer un commentaire