【问题标题】:How do I make my checkbox visible in only some rows of my listview?如何使我的复选框仅在列表视图的某些行中可见?
【发布时间】:2017-09-25 21:23:13
【问题描述】:

我的listview 的每一行都有一个checkbox

listview 中的每一行还显示一个电话号码,位于checkbox 的左侧。

我希望 checkbox 仅在我的 listview 的某些行中可见,其中电话号码 (phoneNumberofContact) 也是匹配联系人的行(电话号码是 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;
    }


}

【问题讨论】:

  • 我的蜘蛛感觉在这条线上刺痛MatchingContactsAsArrayList.contains(phoneNumberofContact)
  • 我在另一个活动中有该行,它按预期工作。
  • 我不知道您在“for”循环中要做什么。 allPhonesofContactsdata 之间没有联系。但是,查看代码,只有当allPhonesofContacts 的最后一个条目在MatchingContactsAsArrayList 中时才能看到检查。它一直循环到最后一个没有break的条目,所以最后一个条目决定检查是可见还是不可见。
  • @Emma 在 for 循环中,我试图查看该人手机上的哪些联系人也在使用我的应用程序,然后在这些联系人旁边放置一个复选框。老实说,我发现整个 getView() 事情很混乱。但是,根据您的评论,我已将代码更改为for (int number = 0; number &lt; MatchingContactsAsArrayList.size(); number++) {...if (MatchingContactsAsArrayList.contains(data.getPhone())),并且我更接近我希望实现的目标。

标签: android listview custom-adapter


【解决方案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);
        } 
       } 

【讨论】:

    【解决方案2】:

    感谢上面的 cmets 和有用的 break; Mike 在他的回答中对代码优化的提示,我想通了。这将在我的MatchingContactsAsArrayList 数组列表中的电话号码旁边给我一个checkbox,并且不在同一数组列表中的电话号码旁边没有复选框。

     //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);
    
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 2016-02-27
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-18
      • 2021-12-07
      相关资源
      最近更新 更多