2017-10-16 48 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,那么你所有的复选框将被默认选中。

相关问题