Blog purpose for android basic example for android app developer. any query please my contact

Thursday 2 April 2020

how to remove selected item from listview in android

In this Article today learn how to remove selected item from listview in android. android studio provides a listview this listview add list item and remove list item so following code how to remove selected item from listview in android. follows the source code. 

 how to clear listview in android:

list item view, grid view, theme, style, design, data binding, room database, volley network library, android studio syntax, java syntax, notification, navigation drawer, wifi, Bluetooth, system permissions, storage, media, seek bar, progress bar, web view,, transition, toolbar, google map, screen, jetpack, work manager, android, fragment, audio manager, activity, action bar and many more.

 

follow the code remove selected item from listview in android:


lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

            @Override

            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

              builder.setMessage(R.string.dialog_message).setTitle(R.string.dialog_title);

                //Setting message manually and performing action on button click

                 builder.setMessage("Do you want to Delete List ?")

                        .setCancelable(false)

                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int id) {

 

                                fruits_list.remove(0);

                                arrayAdapter.notifyDataSetChanged();

                                Toast.makeText(MainActivity.this, "Removed Fruit", Toast.LENGTH_SHORT).show();

                            }

 

                        })

                        .setNegativeButton("No", new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int id) {

                                //  Action for 'NO' Button

                                dialog.cancel();

 

                            }

                        });

                //Creating dialog box

                AlertDialog alert = builder.create();

                //Setting the title manually

                alert.setTitle("Delete List");

                alert.show();

 

 

                return true;

            }

        });

    }

 

In the Adapter to listview in android event listview setOnItemLongClickListener using and delete listview item confirm message to in android.

 

 

1.MainActivity.java:


package com.akash.removelist;

 

import android.content.DialogInterface;

import android.os.Bundle;

import android.app.Activity;

import android.support.v7.app.AlertDialog;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;

import android.widget.Button;

import android.widget.ListView;

import android.widget.Toast;

 

import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

 

public class MainActivity extends Activity {

    AlertDialog.Builder builder;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        // Get reference of widgets from XML layout

        final ListView lv = (ListView) findViewById(R.id.lv);

 

 

        // Initializing a new String Array

        String[] fruits = new String[] {

                "Apple",

                "Bacupari",

                "Beach Plum",

                "Black raspberry",

                "orange"

        };

 

        // Create a List from String Array elements

        final List<String> fruits_list = new ArrayList<String>(Arrays.asList(fruits));

 

        // Create an ArrayAdapter from List

        final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>

                (this, android.R.layout.simple_list_item_1, fruits_list);

 

        // DataBind ListView with items from ArrayAdapter

        lv.setAdapter(arrayAdapter);

 

        builder = new AlertDialog.Builder(this);

        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

            @Override

            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

 

 

                builder.setMessage(R.string.dialog_message).setTitle(R.string.dialog_title);

 

                //Setting message manually and performing action on button click

                builder.setMessage("Do you want to Delete List ?")

                        .setCancelable(false)

                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int id) {

 

                                fruits_list.remove(0);

 

                                arrayAdapter.notifyDataSetChanged();

 

                                Toast.makeText(MainActivity.this, "Removed Fruit", Toast.LENGTH_SHORT).show();

 

 

                            }

 

 

                        })

                        .setNegativeButton("No", new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int id) {

                                //  Action for 'NO' Button

                                dialog.cancel();

 

                            }

                        });

                //Creating dialog box

                AlertDialog alert = builder.create();

                //Setting the title manually

                alert.setTitle("Delete List");

                alert.show();

 

 

                return true;

            }

        });

    }

}

 

 

 

 

2.activity_main.xml:


<RelativeLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/rl"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:padding="10dp"

    tools:context=".MainActivity"

    android:background="#d67f8d"

    >

        <ListView

        android:id="@+id/lv"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        />

</RelativeLayout>

 

 

3.strings.xml:

<resources>

    <string name="app_name">Remove list</string>

    <string name="dialog_message">Delete item?</string>

    <string name="dialog_title">Delete List item</string>

</resources>

 

 

 

4.AndroidManifest.xml:

 

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.akash.removelist">

 

    <application

        android:allowBackup="true"

        android:icon="@mipmap/ic_launcher"

        android:label="@string/app_name"

        android:roundIcon="@mipmap/ic_launcher_round"

        android:supportsRtl="true"

        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

 

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

 

</manifest>

 

 

5.Output:



 

how to remove selected item from listview in android

 




           how to remove selected item from listview in android

 

 


 

 

 

No comments:

Post a Comment