0

Hi am using multiple choice list can any one tell me how should i select all item on any button click event or how unselect all item on button click event

my code is here

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.contact_list);

    findViewsById();
    PhoneContacts pc = new PhoneContacts(ContactList.this);
    pc.readContacts();

    for (int i = 0; i < pc.allPhoneNumbers.size(); i++) {

        _allNumberAndNameMergeList.add(pc.allContactName.get(i) + "\n"
                + pc.allPhoneNumbers.get(i));
    }
    adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_multiple_choice,
            _allNumberAndNameMergeList);
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    listView.setAdapter(adapter);

    button.setOnClickListener(this);
}

private void findViewsById() {
    listView = (ListView) findViewById(R.id.list);
    button = (Button) findViewById(R.id.testbutton);
}

public void onClick(View v) {
    SparseBooleanArray checked = listView.getCheckedItemPositions();
    ArrayList<String> selectedItems = new ArrayList<String>();
    for (int i = 0; i < checked.size(); i++) {
        // Item position in adapter
        int position = checked.keyAt(i);
        // Add sport if it is checked i.e.) == TRUE!
        if (checked.valueAt(i))
            selectedItems.add(adapter.getItem(position));
    }

    String[] outputStrArr = new String[selectedItems.size()];

    for (int i = 0; i < selectedItems.size(); i++) {
        outputStrArr[i] = selectedItems.get(i);
    }


}

}

Hi am using multiple choice list can any one tell me how should i select all item on any button click event or how unselect all item on button click event

1 Answers1

0

I would create custom adapter that extends ArrayAdapter and ListView item that would contain e.g. CheckBox. Than inside adapter class getView() method handle selected items position to get objects on current position and you can do anything you wish. You can have a look at this tutorial - 12. Selecting multiple items in the ListView

http://www.vogella.com/articles/AndroidListView/article.html

Martin
  • 2,146
  • 4
  • 21
  • 32