Remove an item from a ListView using an alert dialog - java

I have this ListView containing items and I want to create an alert dialog that removes any of these items when I long click on an item. onLongClick on an item shows an AlertDialog and if I click yes, it removes the item.
Here is my code.
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
new AlertDialog.Builder(MainActivity.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setMessage("Are You Sure You Want to Delete This Note?!")
.setTitle("Attempt to Delete A Note")
.setPositiveButton("YES", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
try {
notesList.remove(i);
arrayAdapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "ooooooh No!!", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
})
.setNegativeButton("NO", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "Good Choice", Toast.LENGTH_SHORT).show();
}
})
.show();
return true;
}
});

I think the problem is 'i' position of alert dialog click listner and u need to user list item clicked position in order to delete item from list.
Please use below code :
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long l) {
new AlertDialog.Builder(MainActivity.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setMessage("Are You Sure You Want to Delete This Note?!")
.setTitle("Attempt to Delete A Note")
.setPositiveButton("YES", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
try {
if(notesList!=null){
notesList.remove(position);
arrayAdapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "ooooooh No!!", Toast.LENGTH_SHORT).show();
}
}catch (Exception e){
e.printStackTrace();
}
}
})
.setNegativeButton("NO", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "Good Choice", Toast.LENGTH_SHORT).show();
}
})
.show();
return true;
}
});

listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
new AlertDialog.Builder(MainActivity.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setMessage("Are You Sure You Want to Delete This Note?!")
.setTitle("Attempt to Delete A Note")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
try {
dataModels.remove(position);
adapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "ooooooh No!!", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
})
.setNegativeButton("NO", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "Good Choice", Toast.LENGTH_SHORT).show();
}
})
.show();
}
});
This is works for me...try this

Related

Ask dialog to delete an item from listview

I have this code that work perfect to delete an item from listview and it disappear. I want to show dialog to ask if the user want to delete an item.
public void ShowdatainlstView() {
Boodschappenlst.clear();
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
Boodschappenlst);
lstviewProducten.setAdapter(arrayAdapter);
lstviewProducten.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Boodschappenlst.remove(position);
arrayAdapter.notifyDataSetChanged();
}
});
}
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Are you sure?")
.setMessage("do you want to delete this?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("Button Tapped","Yes");
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("Button Tapped","No");
}
})
.show();

Issues in onBackpressed()

Actually my issue is when i press the back button it shows a dialog and also at the same time the application finished without any operation on key? Can any one solve this?
#Override
public void onBackPressed() {
super.onBackPressed();
AlertDialog.Builder ab = new AlertDialog.Builder(this);
ab.setMessage("Really want to exit");
ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
ab.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Main.super.onBackPressed();
}
});
ab.create();
ab.show();
}
remove super.onBackPressed(); from method, keep it in onClick() only
#Override
public void onBackPressed() {
super.onBackPressed(); //remove it
}
seems you are performing same operation in both buttons, you should remove Main.super.onBackPressed() from negativeButton
ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
super.onBackPressed();
}
});
ab.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
ab.dismiss();
}
});
Don't call super method of onBackPressed.
#Override
public void onBackPressed() {
AlertDialog.Builder ab = new AlertDialog.Builder(this);
ab.setMessage("Really want to exit");
ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
ab.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
ab.create();
ab.show();
}
try this
#Override
public void onBackPressed() {
AlertDialog.Builder ab = new AlertDialog.Builder(this);
ab.setMessage("Really want to exit");
ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
ab.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Main.super.onBackPressed();
}
});
ab.create();
ab.show();
}
You need to Remove, super.onBackPressed(); from onBackPressed(). then your function should as,
#Override
public void onBackPressed() {
AlertDialog.Builder ab = new AlertDialog.Builder(this);
ab.setMessage("Really want to exit");
ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
ab.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Main.super.onBackPressed();
}
});
ab.create();
ab.show();
}

Confirmation before deleting from an ArrayList (Android)

I've set up OnContextItemSelected to show an alert when the user tries to delete an item and asks them for confirmation. The problem being that the "confirmationReceived" variable is getting set to true the first time the code is executed with nothing else happening, then when it's executed a second time the item is deleted before the user has a chance to confirm that they want it deleted.
onCreateContextMenu
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Timetable Item");
menu.add(0, Remove_Item, Menu.NONE, R.string.Remove_Item);
}
onContextItemSelected
#Override
public boolean onContextItemSelected(MenuItem item) {
super.onContextItemSelected(item);
AlertDialog diaBox = AskRemoveConfirm();
diaBox.show();
switch (item.getItemId()) {
case (Remove_Item): {
if(confirmationReceived == true) {
AdapterView.AdapterContextMenuInfo menuInfo;
menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int index = menuInfo.position;
removeItem(index);
confirmationReceived = false;
return true;
}
}
}
return false;
}
AskRemoveConfirm()
private AlertDialog AskRemoveConfirm()
{
AlertDialog myRemovalDialogBox =new AlertDialog.Builder(this)
.setTitle("Confirmation")
.setMessage("Are you sure you want to delete this entry?")
.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
confirmationReceived = true;
dialog.dismiss();
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create();
return myRemovalDialogBox;
}
do somthing like this
private AlertDialog AskRemoveConfirm()
{
AlertDialog myRemovalDialogBox =new AlertDialog.Builder(this)
.setTitle("Confirmation")
.setMessage("Are you sure you want to delete this entry?")
.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
confirmationReceived = true;
if(confirmationReceived == true) {
AdapterView.AdapterContextMenuInfo menuInfo;
menuInfo = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
int index = menuInfo.position;
removeItem(index);
confirmationReceived = false;
return true;
}
dialog.dismiss();
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create();
return myRemovalDialogBox;
}

Error when removing item in listView

I got error at this line:
alert.setPositiveButton("YES", new DialogInterface.OnClickListener()
Logcat gives this error
java.lang.NullPointerException
Anyone can help me?
//method to remove list item
private void removeItemFromList(int position) {
final int deletePosition = position;
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
alert.setTitle("Delete");
alert.setMessage("Do you want delete this item?");
alert.setPositiveButton("YES", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TOD O Auto-generated method stub
// main code on after clicking yes
arr.remove(deletePosition);
adapter.notifyDataSetChanged();
adapter.notifyDataSetInvalidated();
}
});
alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alert.show();
}
Call removeAt(deletePosition) onClick of Yes
public void removeAt(int deletePosition) {
arr.remove(deletePosition);
notifyItemRemoved(deletePosition);
notifyItemRangeChanged(deletePosition, arr.size());
}

How to call listview with confirmation dialog?

I want to show confirmation dialog box to user that when user click on yes button open listview to select.
Use this link..............................
http://rajeshvijayakumar.blogspot.in/2013/04/alert-dialog-dialog-with-item-list.html
You can go like this
Listview.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
build = new AlertDialog.Builder(YourActivity.this);
build.setTitle("Title name");
build.setMessage("?");
build.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
//IF yes button pressed then write here
}
dialog.cancel();
}
});
build.setNegativeButton("No", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
dialog.cancel();
}
});
AlertDialog alert = build.create();
alert.show();
return true;
}
});

Categories

Resources