简体   繁体   中英

How to delete selected item from Firebase database?

I'm beginner in Firebase with android application, I succeed to display a ListView of places, now I want to delete a place when I click on an item.

This is my attempt:

listPlace.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, final View view,final int position, long id) {

     final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(EndroitActivity.this);
        dialogBuilder.setTitle("Delete!");
        dialogBuilder.setPositiveButton("Yes...", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Place p = list.get(position);
                System.out.println(p.getNomPlace());

                DatabaseReference dR = FirebaseDatabase.getInstance().getReference("place").child(p.getNomPlace());
                dR.removeValue();
            }
        }).setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(), "Fine, ya coward."
                                , Toast.LENGTH_SHORT).show();
                    }
                });

        dialogBuilder.create().show();

        return true;
    }
});

数据库结构

If you want to delete entire node you have to do like below...

DatabaseReference dR = FirebaseDatabase.getInstance().getReference("place").child("autoKey");
                dR.removeValue();
dR. removeValue();

you have to replace p.getNomPlace() with your autoKey . it will remove the entire node from specific autoKey . getUid() can provide the autokey.

 DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
Query applesQuery = ref.child("place").orderByChild("nomPlace").equalTo(p.getNomPlace());
applesQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    for (DataSnapshot appleSnapshot: dataSnapshot.getChildren()) {
        appleSnapshot.getRef().removeValue();
    }
}

@Override
public void onCancelled(DatabaseError databaseError) {
    Log.e(TAG, "onCancelled", databaseError.toException());
}
});

To remove a record from a Firebase database, you just need to use the removeValue() method directly on the reference. You don't need to use as listener at all. So to remove a particular place, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
rootRef.child("place").child(placeId).removeValue();

In which placeId is the id of a particular place that you want to remove. Note, placeId is not the same with getUid() or user id. To get that id, you need to use getKey() method in the moment you generate that unique key using push() method like this:

String placeId = rootRef.child("place").push().getKey();

Having this placeId , you can use it in any DatabaseReference .

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