2017-09-25 31 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); 

     } 

    } 
相關問題