Quantcast
Channel: niravmehtablog
Viewing all articles
Browse latest Browse all 10

Android Delete Multiple Selected Items in ListView Tutorial

$
0
0

In this tutorial, you will learn how to delete multiple selected items in your listview using a contextual action bar (CAB). A contextual action bar appears at the top of the screen to present actions the user can perform on the currently selected items. The user can select multiple items, deselect items, and continue to navigate within the listview. We will create a listview to show texts and images and on listview long item click will allow the users to select multiple items and on delete button click will remove the selected items. So lets begin…

Create a new project in Eclipse File > New > Android Application Project. Fill in the details and name your project MultipleDeleteListView.

Application Name : MultipleDeleteListView

Project Name : MultipleDeleteListView

Package Name : com.androidbegin.multipledeletelistview

Open your MainActivity.java and paste the following code.

MainActivity.java

In this activity, we have created string arrays with sample data and pass it into the ListViewAdapter class. On listview long item click will allow the user to select multiple items.

Next, create an XML graphical layout for your MainActivity. Go to res > layout > Right Click on layout > New > Android XML File

Name your new XML file listview_main.xml and paste the following code.

listview_main.xml

Next, create an XML graphical layout for your contextual action bar (CAB). Go to res >menu > Right Click on menu New > Android XML File

Name your new XML file activity_main.xml and paste the following code.

activity_main.xml

Next, create an array class. Go to File > New > Class and name it WorldPopulation.java. Select your package named com.androidbegin.multipledeletelistview and click Finish.

Open your WorldPopulation.java and paste the following code.

WorldPopulation.java

Next, create a custom listview adapter. Go to File > New > Class and name it ListViewAdapter.java. Select your package named com.androidbegin.multipledeletelistviewand click Finish.

Open your ListViewAdapter.java and paste the following code.

ListViewAdapter.java

In this custom listview adapter class, string arrays are passed into the ListViewAdapter and set into the TextViews  and ImageViews followed by the positions.

Next, create an XML graphical layout for your listview item. Go to res > layout > Right Click on layout > New > Android XML File

Name your new XML file listview_item.xml and paste the following code.

listview_item.xml

Next, change the application name and texts. Open your strings.xml in your res > values folder and paste the following code.

strings.xml

In your AndroidManifest.xml, set the minimum SDK version to 11. Refer to this link for more info regarding minimum SDK requirements. Open your AndroidManifest.xml and paste the following code.

AndroidManifest.xml


Viewing all articles
Browse latest Browse all 10

Trending Articles