lundi 16 octobre 2017

How do I make my checkbox checked by default in my custom adapter?

In my custom adapter I have an array list of phone numbers, checkedContactsAsArrayList, which I get from my php. In my Android App it looks something like this:

[+12345,+23456,+34567]

I make a string, contactToCheck, from each value of checkedContactsAsArrayList, with :

for (int i = 0; i < checkedContactsAsArrayList.size(); i++) {
    contactToCheck = checkedContactsAsArrayList.get(i);

                                }

The values of contactToCheck will always be present in another array list, MatchingContactsAsArrayList.

For example, MatchingContactsAsArrayList might look like this:

[+12345,+23456,+34567,+45678,+56789,+01234]

A checkbox loads in all cells of my Listview beside these phone numbers, but for the contactToCheck numbers I want the checkbox to be ticked/ checked by default.

Can you tell me what I need to put into my

        if (MatchingContactsAsArrayList.contains(contactToCheck))
        {

    }

for this to happen?

Here is my getView code:

 @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        System.out.println("getView number is :" + i + "convertView is : " + convertView);

        ViewHolder viewHolder = null;

        if (convertView == null) {

            //if there is nothing there (if it's null) inflate the view with the layout
            LayoutInflater li = (LayoutInflater) _c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = li.inflate(R.layout.phone_inflate_listview, null);

            viewHolder = new ViewHolder();
            //      So, for example, title is cast to the name id, in phone_inflate_listview,
            //      phone is cast to the id called no etc
            viewHolder.title = (TextView) convertView.findViewById(R.id.name);
            viewHolder.phone = (TextView) convertView.findViewById(R.id.no);
            viewHolder.invite = (Button)  convertView.findViewById(R.id.btnInvite);
            viewHolder.check = (CheckBox) convertView.findViewById(R.id.checkBoxContact);

            //remember the state of the checkbox
            viewHolder.check.setOnCheckedChangeListener((CompoundButton.OnCheckedChangeListener) _c);

            convertView.setTag(viewHolder);

        } else {

            viewHolder = (ViewHolder) convertView.getTag();

        }
//        store the holder with the view
        final SelectPhoneContact data = (SelectPhoneContact) arraylist.get(i);
        //in the listview for contacts, set the name
        viewHolder.title.setText(data.getName());
        //in the listview for contacts, set the number
        viewHolder.phone.setText(data.getPhone());

        ////*********************

        //for every phone number in the MatchingContactsAsArrayList array list...
        for (int number = 0; number < MatchingContactsAsArrayList.size(); number++) {

                if (MatchingContactsAsArrayList.contains(contactToCheck))
                {


            }

        }


        viewHolder.check.setChecked(data.isSelected());



        viewHolder.check.setTag(data);

        // Return the completed view to render on screen

        return convertView;

    }




Aucun commentaire:

Enregistrer un commentaire