It is necessary to organize the removal from the database of the selected items in the СheckBox in ListView . To do this, wrote this method:

 public void deleteChoose () { SparseBooleanArray sba = listView.getCheckedItemPositions(); for (int i = 0; i < sba.size(); i++) { if(sba.valueAt(i)){ db.delete(TABLE_NAME, COLUMN_ID + "=" + sba.keyAt(i), null); } } } 

In theory, the method should run through the selected items in the listview and remove them from the database, but it does not. There are no syntax errors in the method, but there are logical ones, which seem to be related both with the use of the SparseBooleanArray method and with the formation of the database query itself. On the Internet, I did not find any clear examples of linking listview elements with database elements, although the task is quite common.

  • 2
    The getCheckedItemPositions() method returns the position number in the list, not the ID of the records in the database — these can be different values. You probably need a getCheckedItemIds() method. The method only works for the ListView with the CHOICE_MODE_MULTIPLE attribute. - pavlofff
  • ok, thanks .. then how is it more intelligent to make a comparison of the position number in the list with the ID in the database? - ZigZag
  • 2
    You probably need a method getCheckedItemIds () which returns an array of long [] with the ID of the checked items - pavlofff
  • Thanks, I will try the variant offered by you - ZigZag
  • Thanks pavlofff, for the direction in the right direction. Really rewrote your method using getCheckedItemIds () and it all worked as it should - ZigZag

0