Reputation:
I am trying to delete a child from the database by setting the unique Key to null. What I do is - I press and hold on a List Item to get the Alert Dialog and then press to delete the entry I have selected. The problem is when I retrieve the key it returns me all the keys. And closest I managed to get so far was to delete everything from the database. Please check my code snapshots below:
mLocationListView.setLongClickable(true);
mLocationListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
.setTitle("Delete a Location")
.setMessage("Do you want to delete your location?")
.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Problem is here. if I exclude .child(pushKey)everything is deleted from the database.
DatabaseReference db_node = FirebaseDatabase.getInstance().getReference().getRoot().child("locations").child(pushKey);
db_node.setValue(null);
}
});
AlertDialog alert = builder.create();
alert.show();
return true;
}
});
Here is how I retrieve pushKey. Any suggestions how to do it in more proper way? (if there is one)
queryRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
locationCurrent = dataSnapshot.getValue(LocationCurrent.class);
mLocationAdapter.add(locationCurrent);
pushKey = dataSnapshot.getKey();
Log.i("PUSH KEY ", pushKey);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
locationCurrent = dataSnapshot.getValue(LocationCurrent.class);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
I was thinking of putting the Key to the database as well but there is no point and there must be the most efficient solution.
UPDATE:
I have tried doing this with another method attaching a single value event listener to the query but it does not work because I cannot determine which list item I click on so I delete the whole database. Basically, I need to click on a list item and delete it. But It does not work. I did exactly as in the link here below:
How to delete from firebase realtime database?
Any suggestions?
Upvotes: 1
Views: 4417
Reputation: 4074
Add a transient variable called refKey
in your LocationCurrent.class. Save the ref in onChildAdded
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
locationCurrent = dataSnapshot.getValue(LocationCurrent.class);
locationCurrent.setRefKey(dataSnapshot.getKey());
mLocationAdapter.add(locationCurrent);
}
And on delete click do this :
mLocationListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
DatabaseReference db_node = FirebaseDatabase.getInstance().getReference().child("locations").child(mLocationAdapter.getItem(position).getRefKey());
db_node.removeValue();
});
Upvotes: 2