简体   繁体   中英

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

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

    });

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) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM