Home > Enterprise >  How to add .get(position) in alert dialog box
How to add .get(position) in alert dialog box

Time:09-19

I am using Realtime database and I want to get the position of the selected recycler view item and delete it, the only problem is in this part " String id = list.get(position).getId();" I can't seem to get the position of my selected item.

The error: "Cannot resolve symbol 'position'".

My dialog code:

    private void showDialog() {
    AlertDialog alertDialog = new AlertDialog.Builder(upcoming.this).create();
    alertDialog.setTitle("Confirm Delete");
    alertDialog.setMessage("Are you sure ypu want to delete this item?");

    alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Delete", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            String id = list.get(position).getId();
    Query queryRef = database.child("Reminder").orderByChild("id").equalTo(id);

            queryRef.addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded(DataSnapshot snapshot, String previousChild) {
                    snapshot.getRef().setValue(null);
                }

CodePudding user response:

You've declared your onClick as:

public void onClick(DialogInterface dialog, int which) {

If we look at DialogInterface.OnClickListener, we see that which is documented at:

which int: the button that was clicked (ex. DialogInterface#BUTTON_POSITIVE) or the position of the item clicked.

So the position of the item that was clicked is available in the which parameter, meaning you can use it with:

String id = list.get(which).getId();
  • Related