In android am using multiple choice list for contact selection how to select all contact at one button click

大兔子大兔子 提交于 2019-12-13 06:17:38

问题


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:


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



来源:https://stackoverflow.com/questions/14879045/in-android-am-using-multiple-choice-list-for-contact-selection-how-to-select-all

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!