Mohsen Khalil
Mohsen Khalil

Reputation: 11

Remove an item from a ListView using an alert dialog

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;
    }
}); 

Upvotes: 0

Views: 1385

Answers (2)

Debasish Mondal
Debasish Mondal

Reputation: 239

    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

Upvotes: 0

Nikhil Jogdand
Nikhil Jogdand

Reputation: 57

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;
        }
    }); 

Upvotes: 3

Related Questions