7

I have ListView which is saving all data to database. For adding i have simple button and textBox which adds to database, and show to listView. Now i want thath on long item click (hold on item) will delete the selected item from list. How is possible to do thath (actualy what method to call for long click).

Here is 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(); } } 

2 Answers 2

8

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

Sign up to request clarification or add additional context in comments.

Comments

2

You can use this contruct :D

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.