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();
Related
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
i have this:
public void showDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(updatedActivity.this);
builder.setTitle("Choose the currency you would like to exchange.");
//list of items
String[] items = getResources().getStringArray(R.array.countries_array);
builder.setSingleChoiceItems(items, 0,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
String positiveText = getString(android.R.string.ok);
builder.setPositiveButton(positiveText,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//So here i wanna a code like : If pressbutton = buttonto, then buttonto.setText(selecteditem..)
}
});
String negativeText = getString(android.R.string.cancel);
builder.setNegativeButton(negativeText,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// negative button logic
}
});
AlertDialog dialog = builder.create();
// display dialog
dialog.show();
}
In the on click event, i wanna know which the button was pressed, so for example if buttonto.ispressed then buttonto.settext(the selected item)
So how to do it
Thanks
When I setNegativeButton and setPositiveButton in the alert dialog, when I run it on my mobile device which is a Hauweii phone. runs perfect, but when I run it on the emulator, the alertDialog shows but doesn't display the Ok or cancel but I can still click them, it's like there invisible or something, can anyone please help me?
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
AlertDialog.Builder alertD=new AlertDialog.Builder(Report.this);
alertD.setTitle("Delete?");
alertD.setMessage("Are you sure you want to delete your Bmi ");
final int positionToRemove = position;
alertD.setNegativeButton("Ok", null);
alertD.setPositiveButton("Cancel", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
bmis.remove(positionToRemove);
b.notifyDataSetChanged();
}});
alertD.show();
}
});
Inside your onItemClick
AlertDialog.Builder alertDialog = new AlertDialog.Builder(Report.this);
final int positionToRemove = position;
alertDialog.setTitle("Delete?");
alertDialog.setMessage("Are you sure you want to delete your Bmi "));
// Setting Positive "YES" Button
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
bmis.remove(positionToRemove);
b.notifyDataSetChanged();
dialog.dismiss();
}
});
// Setting Negative "CANCEL" Button
alertDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
// Showing Alert Message
alertDialog.show();
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());
}
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;
}
});