Arslan Ali Arslan Ali - 6 months ago 39
Android Question

Get the position of Selected Item in Action Mode

When i long press any item in the list view then Contextual action bar showed. Now what i want that when i press the delete button after selecting items then the selected items get deleted but I am unable to get the position of selected items.
My code for multi choice mode is:

listViewMessages.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listViewMessages.setMultiChoiceModeListener(new MultiChoiceModeListener() {

@Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
mode.setTitle(listViewMessages.getCheckedItemCount()+ " Selected");

}

@Override
public boolean onActionItemClicked(final ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.menu:
deleteScreenedMessageFromInbox();
mode.finish();
return true;
default:
return false;
}
}

@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate the menu for the CAB
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.contextual, menu);
return true;
}


@Override
public void onDestroyActionMode(ActionMode mode) {

}

@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
});


When i click on delete button then this method is calling:

deleteScreenedMessageFromInbox();


and i have to pass an argument in this method which is the position of selected items. Code for this method is:

public void deleteScreenedMessageFromInbox(Integer position) {

Integer id = smsIDs.get(position);

String deleteScreenedMessage = "delete from " + "sms" + " where " + "id" + " = " + id;

DBsms smsDb = new DBsms(this);
SQLiteDatabase dbw = smsDb.getWritableDatabase();
dbw.execSQL(deleteScreenedMessage);
dbw.close();


any solutions for this?

Answer

Here is what I would suggest:

List<Integer> positionsList = new ArrayList<>();//declare this as a member variable--outside of any method
listViewMessages.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listViewMessages.setMultiChoiceModeListener(new MultiChoiceModeListener() {

@Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
                                  long id, boolean checked) {
   mode.setTitle(listViewMessages.getCheckedItemCount()+ " Selected");
   if(checked)
       positionsList.add(position)
   else
       positionsList.remove(position)
}

@Override
public boolean onActionItemClicked(final ActionMode mode, MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menu:
            deleteScreenedMessageFromInbox();                                       
              mode.finish();
            return true;
        default:
            return false;
    }
}

@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
    // Inflate the menu for the CAB
    MenuInflater inflater = mode.getMenuInflater();
    inflater.inflate(R.menu.contextual, menu);
    return true;
}


@Override
public void onDestroyActionMode(ActionMode mode) {

}

@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
    return false;
}
});

Then on your deleteScreenedMessageFromInbox() access positionsList like so:

public void deleteScreenedMessageFromInbox() {

    DBsms smsDb = new DBsms(this);
    SQLiteDatabase dbw = smsDb.getWritableDatabase();

    for(int position : positionsList) {
        Integer id = smsIDs.get(position);

        String deleteScreenedMessage = "delete from " + "sms" + " where " + "id" + " = " + id;
        dbw.execSQL(deleteScreenedMessage);
    }
    dbw.close();
}