2017-10-16 78 views
-2

在我的自定義適配器中,我有一個電話號碼的數組列表,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,] 

一個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; 

    } 

回答

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

如果您res/layout/phone_inflate_listview.xml只有在這種特殊情況下使用,那麼您可以將android:checked屬性設置爲true,那麼你所有的複選框將被默認選中。

相關問題