Moltehh
Moltehh

Reputation: 297

How to delete item from listView using alertDialog

I have a ListView showing the names of different profiles, when the user holds the click on an item the activity shows an alertDialog, if the user press the confirm button I want to remove the element from my listView, from my ArrayAdapter and from my ArrayList. I know that arg2 in the onItemLongClick method represent the index of the selected item but I want to be able to access it inside the onClick method of the positive button. Any advice? My ArrayList is called "ListaUtentiStringa", the ArrayAdapter is "profilesAdapter" and the listView is called listview. Sorry for my bad english.

listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {


        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setCancelable(true);
        builder.setTitle("Vuoi davvero cancellare il profilo?");
        builder.setPositiveButton("Si", new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int which) {
                                          // How to remove the selected item?
                            }

                        });

builder.setNegativeButton("Annulla", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {

        }

        });

        AlertDialog alert = builder.create();
        alert.show();
        profilesAdapter.notifyDataSetChanged();


    return true;
    }
    }); 

Upvotes: 0

Views: 4603

Answers (2)

Zohra Khan
Zohra Khan

Reputation: 5302

Do this:

   listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {


@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
              final int position, long id) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setCancelable(true);
        builder.setTitle("Vuoi davvero cancellare il profilo?");
        builder.setPositiveButton("Si", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
         // How to remove the selected item?
          adapter.remove(adapter.getItem(position));
        }

    });

Upvotes: 1

Hariharan
Hariharan

Reputation: 24853

Try this..

Use ListaUtentiStringa ArrayList and profilesAdapter adapter as Global variable.

 builder.setPositiveButton("Si", new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int which) {
                                      // How to remove the selected item?

                                ListaUtentiStringa.remove(arg2);
                                profilesAdapter.notifyDataSetChanged();
                                dialog.dismiss();
                        }

                    });

EDIT

public boolean onItemLongClick(AdapterView<?> arg0, View arg1, final int arg2, long arg3) {

Upvotes: -1

Related Questions