I've searched high and low and I'm beginning to think this isn't possible, but I have a custom ListView which is in an inflated layout and put onto a popup window. The lines are made from a layout file using an adapter. Each item of the list is clickable and takes the user to a different list. I want to also make it so that when I long click an item, it selects that item and then allows me to select multiple lines, and then I have a separate button to delete them. To be clear, I don't want a checkbox on each line (there's no room). Just the lines to get highlighted. I'm stuck at the "selects that item" part, let alone the "select multiple lines". I'm going to truncate my code to the relevant parts. Note: masterRecord is the name of the ListView.
private void fillList() {
int numRows = mainDB.numRowsMaster();
if (numRows == 0) {
Toast.makeText(getApplicationContext(), "Nothing to load!", Toast.LENGTH_SHORT).show();
return;
}
cursor = mainDB.getAllRowsMaster();
startManagingCursor(cursor);
String[] fromFieldNames = new String[]{DBAdapter.KEY_MASNAMECOL, DBAdapter.KEY_MASLASTDATECOL, DBAdapter.KEY_MASTOTALTIMEASCLOCKCOL};
int[] toViewIds = new int[]{R.id.rowName, R.id.rowLastDate, R.id.rowTotalTime};
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.master_list_row, cursor, fromFieldNames, toViewIds);
masterRecord.setAdapter(cursorAdapter);
masterRecord.setLongClickable(true);
}
private void listItemClick() {
masterRecord.setOnItemClickListener((parent, viewClicked, position, idInDB) -> {
//Bunch of code here to make the next listview popup and make a cursor adapter. This all works fine.
});
masterRecord.setOnItemLongClickListener((parent, view, position, idInDB) -> {
Toast.makeText(getApplicationContext(), "Is this working?", Toast.LENGTH_SHORT).show();
masterRecord.setClickable(false); //This doesn't make a difference even though it's running.
//The items in masterRecord are still clickable. Separate issue that I'm not worried about ATM.
masterRecord.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
masterRecord.setMultiChoiceModeListener(new ListView.MultiChoiceModeListener() {
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
Toast.makeText(getApplicationContext(), "Here4", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
Toast.makeText(getApplicationContext(), "Here1", Toast.LENGTH_SHORT).show();
return false;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Toast.makeText(getApplicationContext(), "Here3", Toast.LENGTH_SHORT).show();
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Toast.makeText(getApplicationContext(), "Here2", Toast.LENGTH_SHORT).show();
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
Toast.makeText(getApplicationContext(), "Here5", Toast.LENGTH_SHORT).show();
}
});
return true;
});
}
The toasts are just so I can see what's running or not on screen. "Is this working?" is the only thing that shows up when I long press. Nothing gets selected, and if I keep long pressing, it just keeps doing the same thing. If I then short press, it just does the normal short press stuff.
I found the Checkable widget and got all excited, but can't seem to make that work either. The tutorials and other information I've seen have a separate class for the ListView, but that seems like overkill. Is that really the only way to do this? If I missed any relevant code, let me know and I'll edit to include. Thank you.