How to Add and Delete items from ListView in Android || Add and Remove items from ListView in Android - Android Tutorial World

Latest

Welcome to Android Tutorial World Blog, Here you can find all type of post or tutorial related to andorid app development in details.

Sunday, September 29, 2019

How to Add and Delete items from ListView in Android || Add and Remove items from ListView in Android

Add and Remove Items from ListView in Android Example


Hello Friends, Today we will teach you how to add and delete item from listview in android.
In this tutorial we will teach you how to add item on listview by clicking on the button and remove it by long press on item in listview.

Follow the Few Steps

Step 1. :- Add ListView and button in you xml layout file

activity_main.xml :-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView"
        android:layout_above="@+id/btnAdd"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Add Item"
        android:id="@+id/btnAdd"
        android:layout_alignParentBottom="true"/>

</RelativeLayout>

Step 2. :- Now open Java file and make some changes

MainActivity.java :-
package com.akshayarora.myapplication;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private ListView listView;
    private ArrayList<String> arrayList = new ArrayList<>();
    private ArrayAdapter adapter;
    private Button adBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView)findViewById(R.id.listView);
        adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,arrayList);
        listView.setAdapter(adapter);

        adBtn = (Button)findViewById(R.id.btnAdd);
        adBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int items = arrayList.size() + 1;
                arrayList.add("Item "+items);
                adapter.notifyDataSetChanged();
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView adapterView, View view, int position, long l) {
                Toast.makeText(MainActivity.this, ""+adapter.getItem(position), Toast.LENGTH_SHORT).show();
            }
        });
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(final AdapterView<?> adapterView, View view, int i, long l) {

                final int item = i;

                new AlertDialog.Builder(MainActivity.this)
                        .setIcon(android.R.drawable.ic_delete)
                        .setTitle("Are you sure ?")
                        .setMessage("Do you want to delete this item")
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                arrayList.remove(item);
                                adapter.notifyDataSetChanged();
                            }
                        })
                        .setNegativeButton("No",null)
                        .show();
                return true;
            }
        });
    }
}

We are use ArrayAdapter for set ArrayList value.
We Create Alert dialog for showing message when user long press on the item.
If any user press long on the any item than show alert dialog with the message and two button, One button for Yes to delete item from the list and second No for dismiss dialog.

Now run the project and check the output

Output :-


When click on Button


Click Again


 When user Long press on item 2


When user click on Yes button, item remove / delete from the list


Enjoy !

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.