Actually, I want to achieve something like the functionality we mostly see in our cell phones for chats/msgs deletion. When a delete button is clicked on the menu, checkboxes must appear for row selection along with the label on top displaying the no. of selected rows. When "Done/Delete" button on the top right side is clicked after selecting the no. of rows, a delete confirmation dialog box must appear and selected rows must be deleted. I have highlighted the required functionality in images below. [image 1-button in menu ][1][Image 2- multi-mode appearance on clicking the delete button][2][image 3- selection and label][3][image 4- confirmation dialog box][4].This is what I coded so far but my code ain't working, need help. here's my code:
//for the start I want the list be in single mode
ListView listView = (ListView) findViewById(R.id.listView);
ArrayAdapter arrList = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice, namesList);
listView.setAdapter(arrList);
// for the list to be in multimode on clicking the button in the menu
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.mymenu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.deletebutton) {
if (namesList.size() != 0) {
if (mListView.getChoiceMode() == ListView.CHOICE_MODE_SINGLE) {
mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
directoryList = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice,
namesList);
delete();
}
} else {
Toast.makeText(getApplicationContext(), "names not found",
Toast.LENGTH_SHORT).show();
}
}
return super.onOptionsItemSelected(item);
}
// delete method implementation
AlertDialog.Builder a_builder = new AlertDialog.Builder(this);
a_builder.setTitle("Confirm Delete?");
a_builder.setMessage("DELETE")
.setCancelable(true)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int i;
SparseBooleanArray checkedItemPositions = mListView.getCheckedItemPositions();
int itemCount = listView.getCount();
for (i = itemCount - 1; i >= 0; i--) {
if (checkedItemPositions.get(i)) {
arrList.remove(namesList.get(i));
}
}
checkedItemPositions.clear();
arrList.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "Deleted", Toast.LENGTH_SHORT).show();
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
[1]: https://i.stack.imgur.com/ZR1Wv.png
[2]: https://i.stack.imgur.com/4gJGd.png
[3]: https://i.stack.imgur.com/uvn9p.png
[4]: https://i.stack.imgur.com/3gsML.png