2017-09-25 87 views
0

我在我的listview的每一行都有一个checkbox如何让我的复选框仅在我的listview的某些行中可见?

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


} 
+0

我的蜘蛛感官刺痛在此行'MatchingContactsAsArrayList.contains(phoneNumberofContact)' –

+0

我在另一个活动该行并将其按预期工作。 – CHarris

+1

我不能告诉你在“for”循环中要做什么。 'allPhonesofContacts'和'data'之间没有联系。但是,查看代码时,只有当'AllPhonesofContacts'的最后一个条目位于'MatchingContactsAsArrayList'中时才能看到该检查。它循环直到没有“break”的最后一个条目,所以最后一个条目决定了check是否可见或不可见。 – Emma

回答

1

我同意爱玛的评论。唯一的最后数组中的值考虑在内。

此外,我会建议您优化循环,以防止多余的迭代。假设您应显示复选框,如果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); 
     } 
     } 
+0

欢呼迈克,看着它。 – CHarris

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

     } 

    } 
相关问题