0

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;
    }
    }); 
2
  • you can pass the position of your item to your alert dialog , if user presses on delete button then you just have to remove the item at that position !! Commented Apr 12, 2014 at 10:55
  • How can I pass the position? I can't pass arg2 Commented Apr 12, 2014 at 11:03

2 Answers 2

1

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

    });
Sign up to request clarification or add additional context in comments.

1 Comment

I can't make a reference to the position variable, I wrote the error I get in one of the comments
-1

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

5 Comments

But I can't refer to arg2 because "Cannot refer to a non-final variable arg2 inside an inner class defined in a different method"
@Moltehh Use View arg1,final int arg2, long arg3
I get this error if I try using arg2 ("Cannot refer to a non-final variable arg2 inside an inner class defined in a different method")
Just saw your edit; no compilation errors now, maybe it works. Thanks!
Global variables are a poor way to share state between components in an Android app. You should not do that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.