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) {
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()) {
removeListViewItem();
return true;
6. Compile and run!
Other Resources: