I want to delete items on a listView when longpressed. In this code you can add (with edittext) a list item with Button, as you can see below.
I have been unable to write the code for deleting them with a long press. What should I do?
Ideally, a long press will bring up a menu and user can touch and delete the section they want.
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = (EditText) findViewById(R.id.editText); addButton = (Button) findViewById(R.id.addButton); listView = (ListView) findViewById(R.id.listView); listItems = new ArrayList<String>(); listItems.add("First Item - added on Activity Create"); adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems); listView.setAdapter(adapter); addButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { listItems.add(editText.getText().toString()); adapter.notifyDataSetChanged(); } }); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> a, View v, int position, long id) { Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_LONG) .show(); } }); }