如何仅在列表视图的某些行中显示复选框?

时间:2017-09-25 21:23:14

标签: android listview custom-adapter

checkbox的每一行都有一个listview

listview中的每一行都会显示checkbox左侧的电话号码。

我希望checkbox仅显示在listview的某些行中,电话号码(phoneNumberofContact)也是匹配联系人的行(电话号码为1)在MatchingContactsAsArrayList数组列表中。

但我的所有复选框都是隐身的。你能告诉我怎么说对吗?

这是我的自定义适配器中的getView()

  @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        System.out.println("getView number is :" + i + "convertView is : " + convertView);
        //we're naming our convertView as view
        //  View view = 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();

            viewHolder.phone = (TextView) convertView.findViewById(R.id.no);

            viewHolder.check = (CheckBox) convertView.findViewById(R.id.checkBoxContact);
            // viewHolder.check.setVisibility(View.GONE);

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

            convertView.setTag(viewHolder);

        } else {

            viewHolder = (ViewHolder) convertView.getTag();

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

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

        //for every value in the allPhonesofContacts array list, call it phoneNumberofContact
        for (int number = 0; number < allPhonesofContacts.size(); number++) {

            phoneNumberofContact = allPhonesofContacts.get(number);

            System.out.println("SelectPhoneContactAdapter: phoneNumberofContact : " + phoneNumberofContact);

            //if a phone number is in our array of matching contacts
            if (MatchingContactsAsArrayList.contains(phoneNumberofContact))

            {
                viewHolder.check.setVisibility(View.VISIBLE);

            }

            else {
                viewHolder.check.setVisibility(View.INVISIBLE);
            }

        }

        System.out.println("SelectPhoneContactAdapter: allPhonesofContacts : " + allPhonesofContacts.size());


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



        viewHolder.check.setTag(data);

        // Return the completed view to render on screen

        return convertView;

    }

这是我的模特:

public class SelectPhoneContact {

    String phone;

    public String getPhone() {return phone;}

    public void setPhone(String phone) {
        this.phone = phone;
    }

    //*****************************************
    //this is for the checkbox
    boolean selected = false;

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected){
        this.selected=selected;
    }


}

2 个答案:

答案 0 :(得分:1)

我同意艾玛的评论。数组中唯一的 last 值会考虑在内。

另外,我建议您优化循环以防止冗余迭代。如果在MatchingContactsAsArrayList中包含至少一个值,则假设您应该显示复选框。

//for every value in the allPhonesofContacts array list, call it 
 phoneNumberofContact 
    for (int number = 0; number < allPhonesofContacts.size(); number++) {

        phoneNumberofContact = allPhonesofContacts.get(number);

        System.out.println("SelectPhoneContactAdapter: phoneNumberofContact : " + phoneNumberofContact);

        //if a phone number is in our array of matching contacts 
        if (MatchingContactsAsArrayList.contains(phoneNumberofContact)) 

        { 
            viewHolder.check.setVisibility(View.VISIBLE);
            break; // to exit the loop
        } 
         else { 
            viewHolder.check.setVisibility(View.INVISIBLE);
        } 
       } 

答案 1 :(得分:0)

感谢上面的评论和有用的break;提示代码优化迈克在他的回答中我明白了。这将在checkbox阵列列表中的电话号码旁边显示MatchingContactsAsArrayList,并且电话号码旁边没有复选框,不在同一个数组列表中。

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

        //if a phone number is in our array of matching contacts
        if (MatchingContactsAsArrayList.contains(data.getPhone()))

        {
            viewHolder.check.setVisibility(View.VISIBLE);
            System.out.println("it's a match: phoneNumberofContact is : " + data.getPhone());
            break;

        }

        else {

            viewHolder.check.setVisibility(View.INVISIBLE);

        }

    }