-2

Hello everyone i am trying to create an alert dialog box with 2 buttons in it. one to go back to the main screen and the other to remove an item from my list view and go back to the main screen. I have tried a few methods but none seem to work. Can anyone help please? here is my code.

@Override public void onItemClick(AdapterView<?> parent,
                                  View view, int position, long id) {

    AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setTitle("Hello My Name is");
    alertDialog.setMessage("" + mNameList.get(position));
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
        }
    });
    alertDialog.setButton2("Remove", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {

            Toast.makeText(getApplicationContext(), "Removed from list", Toast.LENGTH_SHORT).show();
        }
    });

    alertDialog.show();
}

Any help will be appreciated. Thank you in advance!

  • `but none seem to work` ??? – Simon Jan 18 '15 at 20:42
  • possible duplicate of [Android - Listview delete item and Refresh](http://stackoverflow.com/questions/4656841/android-listview-delete-item-and-refresh) – Simon Jan 18 '15 at 20:43

1 Answers1

0

You can do like this

alertDialog.setButton2("Remove", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        mNameList.remove(position);//or equalalent of remove method
        yourAdapter.notifyDataSetChange();
        Toast.makeText(getApplicationContext(), "Removed from list", Toast.LENGTH_SHORT).show();

    }
});
Esat IBIS
  • 381
  • 1
  • 5
  • 19