Remove all items from listview in android programmatically

How to delete all list view inside values on button click in android app and make listview empty dynamically.

In this tutorial first we are creating a simple listview with multiple items now after that we are removing all the items from listview on button click that’s make the listview completely empty without any values. So here is the complete step by step tutorial for Remove all items from listview in android programmatically.

android-project-download-code-button

How to Remove all items from listview in android programmatically.

Code for MainActivity.java file.

 package com.removeallitemsfromlistview_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.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity {

 ListView listview;
 Button button;
 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);
 button = (Button)findViewById(R.id.button1);
 
 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);
 
 button.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 
 subject_list.clear();
 arrayadapter.notifyDataSetChanged();
 
 
 }
 });
 
 
 }
}

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.removeallitemsfromlistview_android_examples.com.MainActivity" >

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:text="Click here to delete all list items" />

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

</RelativeLayout>

Screenshots:

Remove all items from listview in android programmatically

delete-items-2

Click here to download Remove all items from listview in android programmatically project with source code.