0

I have a custom list view with selectable items.And I am trying to select all items automatically present above the one I selected. For Ex: Suppose there is 10 items in the list view and i selected 5th then it should select all the items available above 5th. i.e(1,2,3,4).

How can i do this?someone please help.I already wasted 1 day in this.

public void onClick(View v) {
    if (v == done) {
        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);
            listView.setItemChecked(position, true);
            // 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);
        }

        Intent intent = new Intent(getApplicationContext(),
                MyBookings.class);

        // Create a bundle object
        Bundle b = new Bundle();
        b.putStringArray("selectedItems", outputStrArr);

        // Add the bundle to the intent.
        intent.putExtras(b);

        // start the ResultActivity
        startActivity(intent);

    }
}
Dev
  • 75
  • 2
  • 11
  • Possible duplicate of [Programmatically select item ListView in Android](http://stackoverflow.com/questions/10788688/programmatically-select-item-listview-in-android) – Murat Karagöz May 04 '17 at 10:17

1 Answers1

1

Use setOnItemClickListener on your ListView, then loop from 0 to the position passed in the method and use

listView.setItemChecked(index, true);
Omar Aflak
  • 2,918
  • 21
  • 39
  • I have updated my question.Could you please show a snippet of code to do this??@Omar Aflak – Dev May 05 '17 at 08:31