Remove/Delete ListView Item using ActionBar Button

How to Remove/Delete ListView Item using ActionBar Button, Step-By-Step

1.  Add a  ListView.

2. Add an Action Bar.

3. Add a variable to save the ListView Item position to your class.

public class MainActivity extends Activity{

int intListViewItemPosition = -1;

    @Override
    public void onCreate(Bundle savedInstanceState) {

4. Add this method to the end of your class. This will Remove selected item from ListView and Refresh/Update a ListView after adding or deleting Item to List, and uses a Toast.

public void removeListViewItem() {
if (intListViewItemPosition != -1) {
myList.remove(intListViewItemPosition);
myArrayAdapter.notifyDataSetInvalidated();
intListViewItemPosition = -1;
} else {
Toast.makeText(this, "No item selected", Toast.LENGTH_SHORT).show();
}
}
5. Add the below bolded line of code to the onOptionsItemSelected method

@Override
public boolean onOptionsItemSelected(MenuItem item) { //needs import android.view.MenuItem;
//super.onOptionsItemSelected(item);
switch (item.getItemId()) {

case R.id.menu_delete:
removeListViewItem();
return true;

6. Compile and run!


Other Resources: