remove item onlongclick from listview - android

I want when long pressing an item of my list to give an option delete and delete the item if it pressed.
//onCreate()
alreadyAddedFood = (ListView) findViewById(R.id.alreadyAddedList);
registerForContextMenu(alreadyAddedFood);
//END of onCreate()
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_general, menu);
menu.setHeaderTitle("Select The Action");
}
#Override
public boolean onContextItemSelected(MenuItem item){
if(item.getItemId()==R.id.delete){
//How to delete?
Toast.makeText(getApplicationContext(),"delete"+item,Toast.LENGTH_LONG).show();
}else{
return false;
}
return true;
}
UPDATE
I also have this class which i implement the onlongClickListener and it works fine but without giving the user the option to press delete like the photo below
public void alreadyAdded(String searchedMessage) {
itemsAdded.add(searchedMessage);
final ArrayAdapter<String>addedAdapter= new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,itemsAdded);
alreadyAddedFood.setAdapter(addedAdapter);
alreadyAddedFood.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
// #Override
// public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
// itemsAdded.remove(position);
// addedAdapter.notifyDataSetChanged();
// Toast.makeText(AddFood.this, "Item Deleted", Toast.LENGTH_LONG).show();
// return true;
// }
// });
}

Add this in your onContextItemSelected :
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); // init the info to get the position from
items.remove(info.position); // remove the item from the list
adapter.notifyDataSetChanged(); //updating the adapter

You can do Something like this:
private ListView ls;
ls.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int arg2, long arg3) {
ls.remove(arg2);//where arg2 is position of item you click
myAdapter.notifyDataSetChanged();
return false;
}
});

You can pop up a dialog and ask the user for his confirmation.
However, it is less aesthetic. I would recommend one of these:
After the long click, show a delete option (using the delete icon rather than text) in the app-bar.
Do not support long click, instead use an swipe-able rows with a delete option (again, with an icon) in the right/left of each row. (I will recommend this library.
(gif was taken from here)

Related

Longclick Issue

I have the following code, which runs when I click on my list, but I need it to only pop up a menu when I long click, the short click does other things.
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId()==R.id.listView1) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_list, menu);
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId()) {
case R.id.add:
// add stuff here
CreateWO.performClick();
return true;
case R.id.punchin:
final Country country = (Country) ListView1.getItemAtPosition(getwoindex());
punchin.performClick();
Toast.makeText(getApplicationContext(), "Work Order="+country.code, Toast.LENGTH_SHORT).show();
return true;
case R.id.punchout:
punchout.performClick();
return true;
default:
return super.onContextItemSelected(item);
}
}
For normal click on listView item use: listView.onItemClickListener and for long click use: listView.setOnItemLongClickListener as follows:
ListView lv =(ListView)findViewById(R.id.my_list);
----
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Do something
}
});
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
//do something on long click
//like show contextMenu
return false;
}
});
EDIT:
For ContextMenu:
You can either call openContextMenu(view) in your onItemLongClick (as above) OR you can use registerForContextMenu(view), for example:
lv.setAdapter(adapter);
registerForContextMenu(lv);
By default contextMenu will open when a user long clicks your view. In this case you do not need to call onLongClick manually on the listView.

Android listview: change context menu item depend on item clicked

I need to change list of menu items(actually hide one or just make it not clickable) depending on list item long-clicks.
I asume I can do it somehow in setOnItemLongClickListener but can't find solution:
public void onCreate(Bundle savedInstanceState){
......
registerForContextMenu(listView);
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> adapterView , View v,int position, long id) {
MyListItem item = (MyListItem) listView.getItemAtPosition(position);
listView.showContextMenu();
return true;
}
});
Just found only solution with global variable to set value in OnItemLongClickListener depend on item clicked values and check it in onCreateContextMenu. Also can save there just clicked item (MyListItem). But I think exists more elegant solution.
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if(!someGlobalVar){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.list_item, menu);
}
...
or
if(!someGlobalVar){
menu.getItem(0).setVisible(someGlobalVar);
}
}

OnCreateContextMenu and ListView items

I have a LisView with several items. To this I've connected an OnItemClickListener (as an inner class), like this:
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(ShoppingListApp02Activity.this, "List item selected:" +
items.get(position).getId(), Toast.LENGTH_LONG).show();
}
});
As is obvious, selecting an entriy displays elements of the object of that entry, in this example the selected Item object's ID (not the list ID, but the objects ID, set when creating the ArrayList items). This works nicely, and enables me to do anything I want with the selected item(s).
Now I'd like to also have a "long-click" listener her, which opens a context menu for the selected ListView item. How do I do that? I've been able to attach an onCreateContextMenu listener to the ListView, but I don't see how I can get the elements of the ArrayList as with the onItemClickListener?
Here's what I've got:
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
menu.add(0, v.getId(), 0, "Something");
menu.add(0, v.getId(), 0, "Something else");
}
Since OnCreateConextMenu takes different parameters than the OnItemClickListener, how to I access the ArrayList's elements like in the OnItemClickListener?
If you decide you still want to use the context menu paradigm:
Consider this for working with lists:
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
// Get the list
ListView list = (ListView)v;
// Get the list item position
AdapterContextMenuInfo info = (AdapterContextMenuInfo)menuInfo;
int position = info.position
// Now you can do whatever.. (Example, load different menus for different items)
list.getItem(position);
...
}
Instead of messing with context menus (which are used in a wide context - like right-click in PC), ListView offers onItemLongClick event which is a lot more easier to implement. For example:
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
return false;
}
});
This will help you to achieve long-pressed actions on a row.
Open the context menu of the view within the event handler for the long press event on the row view.
convertView.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
((Activity)mContext).openContextMenu(v);
return true;
}
});
This way both the click on the view and the long press context menu works on the listview row item.
First register context menu into your listview to open context menu:
registerForContextMenu(YOUR LIST-VIEW OBJECT);
then you can use your onCreateContextMenu() method:
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
menu.add(0, v.getId(), 0, "Something");
menu.add(0, v.getId(), 0, "Something else");
}
You actually dont need to use longClickListener with your listview to use ContextMenu.
Hope it will help you.
I will let you go through the below written example and see how it is implemented by using onContextItemSelected()
public void onCreateContextMenu(ContextMenu menu,
View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle(title);
menu.add(0, CMD_EDIT, 0, R.string.context_menu_edit);
menu.add(0, CMD_DELETE, 0, R.string.context_menu_delete);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case CMD_EDIT:
any_function();//add your functionality here i.e. what you want to do
return true;
case CMD_DELETE:
**confirmDelete**();
return true;
default:
return super.onContextItemSelected(item);
}
}
Hope this helps...
Try this for a View item in recycleView
viewForContextMenu.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.add("Context Menu Item1").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
//What should happent on selecting context menu item 1
return true;
}
});
}
});
You can use it with setting data to a ViewHolder item

Long click on ListFragment

I'm working with a ListFragment and doing a onListItemClick. Everything works fine, but now I want to use a long Item Click (e.g setOnItemLongClickListener(new OnItemLongClickListener() for an Activity). How can I use this in my fragment?
Thanks!
Yes, the solution posted by tsync works for me. I too had ran into same problem and considered that this is not possible. I tried the above suggestion as follows:
public class ProjectsFragment extends ListFragment {
#Override
public void onActivityCreated(Bundle savedState) {
super.onActivityCreated(savedState);
getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Toast.makeText(getActivity(), "On long click listener", Toast.LENGTH_LONG).show();
return true;
}
});
and it worked!
Depending on what you want to realize you can use the given methods for context menues:
First register the View class which gets long pressed (inside your Fragment class):
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
registerForContextMenu(this.getListView());
}
Than implement these two methods, to create a context menu and do what ever you want when a menu item is clicked:
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = this.getActivity().getMenuInflater();
inflater.inflate(R.menu.my_context_menu, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.add: // <-- your custom menu item id here
// do something here
return true;
default:
return super.onContextItemSelected(item);
}
}
This works for me
getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> av, View v, int position, long id) {
//Get your item here with the position
return true;
}
});

How to handle long tap on ListView item?

How can I catch such event? onCreateContextMenu is quite similiar, but I don't need menu.
It's hard to know what you need to achieve. But my guess is that you want to perform some acion over the item that receives the long click. For that, you have two options:
add an AdapterView.OnItemLongClickListener. See setOnItemLongClickListener.
.
listView.setOnItemLongClickListener (new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView parent, View view, int position, long id) {
//do your stuff here
}
});
if you are creating a custom adapter, add a View.OnLongClickListener when creating the View in the method Adapter#getView(...)
Normally, you'd associate a long click on a list view with a context menu, which you can do by registering the listView with Activity.registerForContextMenu(View view) to give a more consistent user interface experience with other android apps.
and then override the onContextItemSelected method in your app like this:
#Override
public void onCreate(Bundle savedInstanceState) {
listView = (ListView) findViewById(R.id.your_list_view);
registerForContextMenu(listView);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle(getString(R.string.menu_context_title));
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.your_context_menu, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.some_item:
// do something useful
return true;
default:
return super.onContextItemSelected(item);
}
The position in the list is also held in info.id
If you just want to capture the long click event, then I think what Snicolas is suggesting would work.
Add a custom View.OnLongClickListener to your views. It can be shared by many instances, then you can use the parameter of
onLongClick(View v)
to know which view has been clicked and react accordingly.
Regards,
Stéphane
//Deleted individual cart items
//on list view cell long press
cartItemList.setOnItemLongClickListener (new OnItemLongClickListener() {
#SuppressWarnings("rawtypes")
public boolean onItemLongClick(AdapterView parent, View view, final int position, long id) {
final CharSequence[] items = { "Delete" };
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Action:");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
cart = cartList.get(position);
db.removeProductFromCart(context, cart);
new AlertDialog.Builder(context)
.setTitle(getString(R.string.success))
.setMessage(getString(R.string.item_removed))
.setPositiveButton("Done", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(CartDetailsActivity.this, HomeScreen.class);
startActivity(intent);
}
})
.show();
}
});
AlertDialog alert = builder.create();
alert.show();
//do your stuff here
return false;
}
});

Categories

Resources