Remove selected listview item in android on long click listener

How to delete only clicked item from listview while user long press that list element.

In this tutorial we are creating a listview with multiple items and now we are deleting the selected long press list element using setOnItemLongClickListener() method. So here is the complete step by step tutorial for Remove selected listview item in android on long click listener.

android-project-download-code-button

How to Remove selected listview item in android on long click listener.

Code for MainActivity.java file.

 package com.removeselectedlistviewitem_android_examples.com;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {

 ListView listview;
 String[] subjects = new String[] {
 "Android",
 "PHP",
 "Blogger",
 "WordPress",
 "SEO"
 };
 List<String> subject_list;
 ArrayAdapter<String> arrayadapter;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 listview = (ListView)findViewById(R.id.listView1);
 
 subject_list = new ArrayList<String>(Arrays.asList(subjects));

 arrayadapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, subject_list);

 listview.setAdapter(arrayadapter);
 
 listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

 @Override
 public boolean onItemLongClick(AdapterView<?> parent, View view,
 int position, long id) {
 // TODO Auto-generated method stub
 
 subject_list.remove(position);
 
 arrayadapter.notifyDataSetChanged();
 
 Toast.makeText(MainActivity.this, "Item Deleted", Toast.LENGTH_LONG).show();
 
 return true;
 }
 
 });
 
 }

 
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.removeselectedlistviewitem_android_examples.com.MainActivity" >

 <ListView
 android:id="@+id/listView1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true" >
 </ListView>

</RelativeLayout>

Screenshots:

before-delete

Remove selected listview item in android on long click listener

Click here to download Remove selected listview item in android on long click listener project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *