Alex
Alex

Reputation: 1934

remove item onlongclick from listview

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;
//            }
//        });
    }

enter image description here

Upvotes: 1

Views: 197

Answers (3)

Matan Kintzlinger
Matan Kintzlinger

Reputation: 148

You can pop up a dialog and ask the user for his confirmation. However, it is less aesthetic. I would recommend one of these:

  1. After the long click, show a delete option (using the delete icon rather than text) in the app-bar.
  2. 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.

picture taken from the library page on github (gif was taken from here)

Upvotes: 0

Oussema Aroua
Oussema Aroua

Reputation: 5339

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

Upvotes: 1

chand mohd
chand mohd

Reputation: 2550

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;
        }
    });

Upvotes: 0

Related Questions