如何在我的自定义适配器中默认选中复选框?

时间:2017-10-16 09:26:37

标签: android checkbox custom-adapter

在我的自定义适配器中,我有一个电话号码的数组列表,checkedContactsAsArrayList,我从php获得。在我的Android应用程序中,它看起来像这样:

[+12345,+23456,+34567]

我从contactToCheck的每个值创建一个字符串checkedContactsAsArrayList,其中包含:

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

                                }

contactToCheck的值将始终存在于另一个数组列表MatchingContactsAsArrayList中。

例如,MatchingContactsAsArrayList可能如下所示:

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

checkbox的所有单元格中都会Listview加载这些电话号码,但对于contactToCheck号码,我希望默认情况下勾选/选中复选框。

你能告诉我我需要把它放进我的

吗?
        if (MatchingContactsAsArrayList.contains(contactToCheck))
        {

    }

要发生这种情况吗?

这是我的getView代码:

 @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;

    }

2 个答案:

答案 0 :(得分:0)

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

          //if a phone number is in our array of checked contacts
            if (checkedContactsAsArrayList.contains(data.getPhone())) {

                viewHolder.check.setChecked(true);
            }
        }

答案 1 :(得分:0)

如果您的res/layout/phone_inflate_listview.xml仅用于此特定情况,则可以将android:checked属性设置为true,然后默认情况下会选中所有复选框。