When deleting a long click

I have a ListView that stores all the data in a database. To add, I have a simple button and a text block that adds to the database and shows a listView. Now I want that with a long click on an item (hold the item) will remove the selected item from the list. How can this be done (really, which method to call for a long click).

Here is the current code:

import java.util.List; import java.util.Random; import android.app.ListActivity; import android.os.Bundle; import android.text.Editable; import android.view.View; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.ListView; public class Announce extends ListActivity{ private CommentsDataSource datasource; EditText edit; ListView list; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.announce); datasource = new CommentsDataSource(this); datasource.open(); List<Comment> values = datasource.getAllComments(); ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this, android.R.layout.simple_list_item_1, values); setListAdapter(adapter); } public void onClick(View view) { @SuppressWarnings("unchecked") ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>) getListAdapter(); Comment comment = null; switch (view.getId()) { case R.id.add: edit = (EditText)findViewById(R.id.editTxt); Editable txt=(Editable)edit.getText(); String input = txt.toString(); comment = datasource.createComment(input); adapter.add(comment); break; } adapter.notifyDataSetChanged(); } @Override protected void onResume() { datasource.open(); super.onResume(); } @Override protected void onPause() { datasource.close(); super.onPause(); } } 
+6
source share
2 answers

You want the context menu basically look here: http://developer.android.com/guide/topics/ui/menus.html#context-menu

+8
source

You can use this principle: D

 something.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { ... } }); 
+2
source

Source: https://habr.com/ru/post/912667/


All Articles