简体   繁体   中英

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

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

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

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