Select and delete multiple items in a list in Sketchware android project
1. In image manager of project, add delete icon ic_delete_white.
2. In Resource, in menu folder, add an xml file named context_menu.xml. Put following code in this file:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/delete"
android:title="Delete"
android:icon="@drawable/ic_delete_white"
android:showAsAction="always"/>
</menu>
3. Create a new custom variable of type SparseBooleanArray with name selectedItems and define it as new SparseBooleanArray().
4. Put following codes in onCreate. Change names of variables as in your project.
// Enable multiple selection mode with contextual action bar
listview1.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
// Handle selection events via MultiChoiceModeListener
listview1.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
// Update the selection map based on user interaction
if (checked) {
selectedItems.put(position, true); // Mark item as selected
} else {
selectedItems.delete(position); // Unmark item
}
// Refresh the adapter to visually highlight selected items
((BaseAdapter)listview1.getAdapter()).notifyDataSetChanged();
// Update the ActionMode title with the number of selected items
mode.setTitle(selectedItems.size() + " selected");
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate the contextual menu (e.g., delete option)
getMenuInflater().inflate(R.menu.context_menu, menu);
return true;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// Handle clicks on contextual menu items
if (item.getItemId() == R.id.delete) {
// Loop backwards to safely remove selected items
for (int i = listview1.getCount() - 1; i >= 0; i--) {
if (selectedItems.get(i)) {
maplist.remove(i); // Remove item from data list
}
}
// Clear selection and update UI
selectedItems.clear();
((BaseAdapter)listview1.getAdapter()).notifyDataSetChanged();
// Save updated list to SharedPreferences as JSON
sp.edit().putString("data", new Gson().toJson(maplist)).commit();
// Close action mode
mode.finish();
return true;
}
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
// Clear selection and refresh UI when action mode ends
selectedItems.clear();
((BaseAdapter)listview1.getAdapter()).notifyDataSetChanged();
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// Not used here, return false
return false;
}
});
5. In onBindCustomView, if selectedItems.get(_position, false), change background colour of outermost linear in custom view.
6. Save and run the project.
Sunrise Technologies builds secure, scalable, and intelligent solutions tailored to your organisation’s unique needs. From process automation to system integration, we help Australian enterprises drive efficiency and innovation with future-ready technology.
ReplyDeleteE-commerce Software Developer in Australia
AI is reshaping industries globally. In healthcare, it enables faster diagnosis and personalized treatment plans. In e-commerce, AI improves product recommendations, enhancing user experience. Best AI Application Development Company
ReplyDeleteI am a learner using mobile,onstart part is showing in video, here not discussed and facing trouble to download Gson.Help me
ReplyDeleteIn onStart, I update the List map and refresh ListView, when any new item is added.
Delete