2016-12-26 128 views
0

我使用列表視圖與複選框獲取手機聯繫人。我使用了這個link給出的指導。如果我想選擇一些聯繫人,代碼工作正常。現在我想在活動爲複選框選擇列表視圖的所有選項

// Logcat 
 
FATAL EXCEPTION: main 
 
Process: com.sam.afrikk, PID: 10628 
 
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 
 
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) 
 
at java.util.ArrayList.get(ArrayList.java:308) 
 
at com.sam.afrikk.ContactsPickerActivity$1.onCheckedChanged(ContactsPickerActivity.java:54) 
 
at android.widget.CompoundButton.setChecked(CompoundButton.java:156) 
 
at android.widget.CompoundButton.toggle(CompoundButton.java:115) 
 
at android.widget.CompoundButton.performClick(CompoundButton.java:120) 
 
at android.view.View$PerformClick.run(View.java:21534) 
 
at android.os.Handler.handleCallback(Handler.java:815) 
 
at android.os.Handler.dispatchMessage(Handler.java:104) 
 
at android.os.Looper.loop(Looper.java:207)

添加一個「全選」複選框,但我發現IndexOutOfBoundsException異常。

public class ContactsList{ 
 

 
    public ArrayList<Contact> contactArrayList; 
 

 
    ContactsList(){ 
 

 
     contactArrayList = new ArrayList<Contact>(); 
 
    } 
 

 

 

 
    public int getCount(){ 
 

 
     return contactArrayList.size(); 
 
    } 
 

 
    public void addContact(Contact contact){ 
 
     contactArrayList.add(contact); 
 
    } 
 

 

 

 
    public void removeContact(Contact contact){ 
 
     contactArrayList.remove(contact); 
 
    } 
 

 
    public Contact getContact(int id){ 
 

 
     for(int i=0;i<this.getCount();i++){ 
 
      if(Integer.parseInt(contactArrayList.get(i).id)==id) 
 
       return contactArrayList.get(i); 
 
     } 
 

 
     return null; 
 
    } 
 

 

 

 

 
}
public class ContactsListAdapter extends BaseAdapter { 
 

 
    Context context; 
 
    ContactsList contactsList,filteredContactsList,selectedContactsList; 
 
    String filterContactName; 
 

 
    ContactsListAdapter(Context context, ContactsList contactsList){ 
 

 
     super(); 
 
     this.context = context; 
 
     this.contactsList = contactsList; 
 
     this.filteredContactsList=new ContactsList(); 
 
     this.selectedContactsList = new ContactsList(); 
 
     this.filterContactName = ""; 
 
    } 
 

 
    public void filter(String filterContactName){ 
 

 

 

 
     filteredContactsList.contactArrayList.clear(); 
 

 
     if(filterContactName.isEmpty() || filterContactName.length()<1){ 
 
      filteredContactsList.contactArrayList.addAll(contactsList.contactArrayList); 
 
      this.filterContactName = ""; 
 

 
     } 
 
     else { 
 
      this.filterContactName = filterContactName.toLowerCase().trim(); 
 
      for (int i = 0; i < contactsList.contactArrayList.size(); i++) { 
 

 
       if (contactsList.contactArrayList.get(i).name.toLowerCase().contains(filterContactName)) 
 
        filteredContactsList.addContact(contactsList.contactArrayList.get(i)); 
 
      } 
 
     } 
 
     notifyDataSetChanged(); 
 

 
    } 
 

 
    public void addContacts(ArrayList<Contact> contacts){ 
 
     this.contactsList.contactArrayList.addAll(contacts); 
 
     this.filter(this.filterContactName); 
 

 
    } 
 

 
    @Override 
 
    public int getCount() { 
 
     return filteredContactsList.getCount(); 
 
    } 
 

 
    @Override 
 
    public Contact getItem(int position) { 
 
     return filteredContactsList.contactArrayList.get(position); 
 
    } 
 

 
    @Override 
 
    public long getItemId(int position) { 
 
     return Long.parseLong(this.getItem(position).id); 
 
    } 
 

 
    @Override 
 
    public View getView(int position, View convertView, ViewGroup parent) { 
 

 
     ViewHolder viewHolder; 
 

 
     if(convertView==null){ 
 
      LayoutInflater inflater = ((Activity)context).getLayoutInflater(); 
 

 
      convertView = inflater.inflate(R.layout.contact_item, parent, false); 
 

 
      viewHolder = new ViewHolder(); 
 
      viewHolder.chkContact = (CheckBox) convertView.findViewById(R.id.chk_contact); 
 
      convertView.setTag(viewHolder); 
 

 
     }else { 
 
      viewHolder = (ViewHolder) convertView.getTag(); 
 
     } 
 

 
     viewHolder.chkContact.setText(this.filteredContactsList.contactArrayList.get(position).toString()); 
 
     viewHolder.chkContact.setId(Integer.parseInt(this.filteredContactsList.contactArrayList.get(position).id)); 
 
     viewHolder.chkContact.setChecked(alreadySelected(filteredContactsList.contactArrayList.get(position))); 
 

 
     viewHolder.chkContact.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
 
      @Override 
 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
 
       Contact contact = filteredContactsList.getContact(buttonView.getId()); 
 

 
       if(contact!=null && isChecked && !alreadySelected(contact)){ 
 
        selectedContactsList.addContact(contact); 
 
       } 
 
       else if(contact!=null && !isChecked){ 
 
        selectedContactsList.removeContact(contact); 
 
       } 
 
      } 
 
     }); 
 

 
     return convertView; 
 
    } 
 

 
    public boolean alreadySelected(Contact contact) 
 
    { 
 
     if(this.selectedContactsList.getContact(Integer.parseInt(contact.id))!=null) 
 
      return true; 
 

 
     return false; 
 
    } 
 

 

 
    public static class ViewHolder{ 
 

 
     CheckBox chkContact; 
 
    } 
 
}
public class ContactsPickerActivity extends AppCompatActivity { 
 

 
    ListView contactsChooser; 
 
    Button btnDone; 
 
    TextView txtLoadInfo; 
 
    ContactsListAdapter contactsListAdapter; 
 
    ContactsLoader contactsLoader; 
 
    CheckBox selectAll; 
 
    Contact contact; 
 
    ContactsList contactsList; 
 

 
    @Override 
 
    protected void onCreate(Bundle savedInstanceState) { 
 
     super.onCreate(savedInstanceState); 
 
     setContentView(R.layout.activity_contacts_picker); 
 

 
     contactsChooser = (ListView) findViewById(R.id.lst_contacts_chooser); 
 
     btnDone = (Button) findViewById(R.id.btn_done); 
 
     txtLoadInfo = (TextView) findViewById(R.id.txt_load_progress); 
 
     selectAll = (CheckBox)findViewById(R.id.selectAll); 
 

 
     contactsListAdapter = new ContactsListAdapter(this,new ContactsList()); 
 

 
     contactsChooser.setAdapter(contactsListAdapter); 
 

 

 
     loadContacts(""); 
 

 
     selectAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
 
      @Override 
 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
 
       contactsList = new ContactsList(); 
 

 

 
       if(isChecked){ 
 
        selectAll.setText("Unselect All"); 
 
\t \t \t \t \t //HERE I"M GETTING EXCEPTION 
 
        for (int i=0;i<contactsListAdapter.getCount();i++){ 
 
         contact = contactsListAdapter.selectedContactsList.contactArrayList.get(i); 
 
         contactsListAdapter.selectedContactsList.addContact(contact); 
 
        } 
 
       }else{ 
 
        selectAll.setText("Select All"); 
 
        for (int i=0;i<contactsListAdapter.getCount();i++){ 
 
         contact = contactsListAdapter.selectedContactsList.contactArrayList.get(i); 
 
         contactsListAdapter.selectedContactsList.removeContact(contact); 
 
        } 
 

 
       } 
 
      } 
 
     }); 
 

 
     btnDone.setOnClickListener(new View.OnClickListener() { 
 
      @Override 
 
      public void onClick(View v) { 
 

 
       if(contactsListAdapter.selectedContactsList.contactArrayList.isEmpty()){ 
 
        setResult(RESULT_CANCELED); 
 
       } 
 
       else{ 
 

 
        Intent resultIntent = new Intent(); 
 

 
        resultIntent.putParcelableArrayListExtra("SelectedContacts", contactsListAdapter.selectedContactsList.contactArrayList); 
 
        setResult(RESULT_OK,resultIntent); 
 

 
       } 
 
       finish(); 
 

 
      } 
 
     }); 
 
    } 
 

 

 

 
    private void loadContacts(String filter){ 
 

 
     if(contactsLoader!=null && contactsLoader.getStatus()!= AsyncTask.Status.FINISHED){ 
 
      try{ 
 
       contactsLoader.cancel(true); 
 
      }catch (Exception e){ 
 

 
      } 
 
     } 
 
     if(filter==null) filter=""; 
 

 
     try{ 
 
      //Running AsyncLoader with adapter and filter 
 
      contactsLoader = new ContactsLoader(this,contactsListAdapter); 
 
      contactsLoader.txtProgress = txtLoadInfo; 
 
      contactsLoader.execute(filter); 
 
     }catch(Exception e){ 
 
      e.printStackTrace(); 
 
     } 
 
    } 
 

 

 

 

 
}

回答

0

有一個有兩個問題:

  1. 你遍歷聯繫人的完整列表,而不是僅在選定的聯繫人列表: (我也已將迭代器從for循環更改爲數組迭代器,這會使代碼更具可讀性)
  2. 在取消選擇塊中,您調用的代替removeContact
Array<Contact> selectedContactsArray = contactsListAdapter.selectedContactsList.contactArrayList; 
for (Contact contact : selectedContactsArray) { 
    contactsListAdapter.selectedContactsList.removeContact(contact); 
} 
相關問題