2017-10-16 3 views
-2

私のカスタムアダプターでは、から得られるcheckedContactsAsArrayListという電話番号の配列リストを持っています。 contactToCheckの値は常に別の配列中に存在するであろうカスタムアダプタでチェックボックスをデフォルトでオンにするにはどうすればよいですか?

for (int i = 0; i < checkedContactsAsArrayList.size(); i++) { 
    contactToCheck = checkedContactsAsArrayList.get(i); 

           } 

[+12345,+23456,+34567] 

は、私は、checkedContactsAsArrayListの各値から、文字列、contactToCheckを作る:私のAndroidのアプリで、それは次のようになりますリスト、MatchingContactsAsArrayList。例えば

MatchingContactsAsArrayListは次のようになります。

[+12345,+23456,+34567,+45678,+56789,] 

これらの電話番号の横にあるが、私はチェックボックスはデフォルトでチェック/チェックされたいcontactToCheck番号についての私のListviewのすべてのセルでcheckbox負荷を。

私はこれを実現するための私の

 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のみ、あなたはtrueandroid:checked属性を設定することができ、この特定のケースで使用され、その後、すべてのチェックボックスがデフォルトでチェックされます。

関連する問題