长按longclick事件期间如何实现警报对话框?

时间:2019-03-27 08:19:57

标签: java android firebase-realtime-database

我想在listview的longpress事件期间在我的代码中实现一个警告对话框,以便仅在按Okay时才能删除。删除按预期方式工作,但是实施对话框将使其更加令人满意。任何帮助将不胜感激。

我需要为以下过程实现警报对话框。

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {


            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                mDatabase.child("users").child(mUserId).child("items")
                        .orderByChild("title")
                        .equalTo((String) listView.getItemAtPosition(position))
                        .addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                if (dataSnapshot.hasChildren()) {
                                    DataSnapshot firstChild = dataSnapshot.getChildren().iterator().next();
                                    firstChild.getRef().removeValue();
                                }
                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {

                            }
                        });
            return  true;}
        });
    }
}

编辑

       listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

        public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
        final AlertDialog alertDialog;
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(EditActivity.this);
        alertDialogBuilder.setMessage("Are you sure, You wanted to delete?");
        alertDialogBuilder.setPositiveButton("yes",
        new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface arg0, int arg1) {
        mDatabase.child("users").child(mUserId).child("items")
        .orderByChild("title")
        .equalTo((String) listView.getItemAtPosition(position))
        .addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.hasChildren()) {
        DataSnapshot firstChild = dataSnapshot.getChildren().iterator().next();
        firstChild.getRef().removeValue();
        alertDialog.dismiss();
        }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
        });
        }
        });
        alertDialogBuilder.setNegativeButton("No",null);

        alertDialog = alertDialogBuilder.create();
        alertDialog.show();
        return  true;}
            }
            );

1 个答案:

答案 0 :(得分:0)

def invert_dict(d):
    print(' the dictionary is \n' , d)
    inverse = dict()
    for key in d:
        val = d[key]
        for i in val:
            if i not in inverse:
                inverse[i] = [key]
            else:
                inverse[i].append(key)
    return inverse
d={'vowels':['a','e','i','o','u'], 'letters':['a','b','c','d']}
print(invert_dict(d))