New Android Project Select
Click On ListView Then Selected Value Display
Long Press on Value When Delete Dialog Display
Project Name : ListviewSample
Build Target : Android 2.2
Application Name : ListviewSample
Package Name : com.ovte.listviewsample
Create Activity : ListviewSample
Min SDK Version : 8
Open AndroidManifest.xml File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ovte.listviewsample"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="ListviewSample"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ovte.listviewsample"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="ListviewSample"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
Open ListviewSample.java File
package com.ovte.listviewsample;
import java.util.ArrayList;
import java.util.Arrays;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
public class ListviewSample extends ListActivity {
/** We want to display these names as our list items */
String[] names = {
"Piyush","Ankit","Dipak","Hitesh","Renuka","Dharmesh","Mitesh","Mihir","Lalit","Kiran","Rohit","Krunal"
};
private ArrayAdapter<String> adapter; // The list adapter
private String selectedItem; // Stores the selected list item
private final Context context = this; // This context
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create the listener for normal item clicks
OnItemClickListener itemListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long rowid) {
Toast.makeText(
getApplicationContext(),
"You have clicked on " + parent.getItemAtPosition(position).toString() + ".",
Toast.LENGTH_SHORT).show();
}
};
// Create the listener for long item clicks
OnItemLongClickListener itemLongListener = new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long rowid) {
// Store selected item in global variable
selectedItem = parent.getItemAtPosition(position).toString();
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Do you want to remove " + selectedItem + "?");
builder.setCancelable(false);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
adapter.remove(selectedItem);
adapter.notifyDataSetChanged();
Toast.makeText(
getApplicationContext(),
selectedItem + " has been removed.",
Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Create and show the dialog
builder.show();
// Signal OK to avoid further processing of the long click
return true;
}
};
// Convert array to ArrayList
ArrayList<String> list = new ArrayList<String>(Arrays.asList(names));
// Create new ArrayAdapter with the ArrayList as underlying data source
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list);
// Assign adapter to the list
setListAdapter(adapter);
// Assign both click listeners
getListView().setOnItemClickListener(itemListener);
getListView().setOnItemLongClickListener(itemLongListener);
}
}
import java.util.ArrayList;
import java.util.Arrays;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
public class ListviewSample extends ListActivity {
/** We want to display these names as our list items */
String[] names = {
"Piyush","Ankit","Dipak","Hitesh","Renuka","Dharmesh","Mitesh","Mihir","Lalit","Kiran","Rohit","Krunal"
};
private ArrayAdapter<String> adapter; // The list adapter
private String selectedItem; // Stores the selected list item
private final Context context = this; // This context
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create the listener for normal item clicks
OnItemClickListener itemListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long rowid) {
Toast.makeText(
getApplicationContext(),
"You have clicked on " + parent.getItemAtPosition(position).toString() + ".",
Toast.LENGTH_SHORT).show();
}
};
// Create the listener for long item clicks
OnItemLongClickListener itemLongListener = new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long rowid) {
// Store selected item in global variable
selectedItem = parent.getItemAtPosition(position).toString();
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Do you want to remove " + selectedItem + "?");
builder.setCancelable(false);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
adapter.remove(selectedItem);
adapter.notifyDataSetChanged();
Toast.makeText(
getApplicationContext(),
selectedItem + " has been removed.",
Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Create and show the dialog
builder.show();
// Signal OK to avoid further processing of the long click
return true;
}
};
// Convert array to ArrayList
ArrayList<String> list = new ArrayList<String>(Arrays.asList(names));
// Create new ArrayAdapter with the ArrayList as underlying data source
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list);
// Assign adapter to the list
setListAdapter(adapter);
// Assign both click listeners
getListView().setOnItemClickListener(itemListener);
getListView().setOnItemLongClickListener(itemLongListener);
}
}
Output
Application Run Then Default Display This Type
Long Press on Value When Delete Dialog Display