I\'m currently working on Adding Friends with the help of firebase RecyclerView in which if a user tap on ADD button, he is added in database n that tapped item is needed to
If you look at this code, it removes a Note from Firebase when the Note is clicked. It seems straight forward if you understand a Firebase query. This will also update the RecyclerView once the RecyclerView adapter is setup correctly. You don't need a List of data, just the value of what you want to remove, like an ID or a key.
@Override
public void onLongClick(View v, int i) {
mquery.orderByChild("text")
.equalTo((String) notes.get(i).getName())
.addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChildren()) {
DataSnapshot firstChild = dataSnapshot.getChildren().iterator().next();
firstChild.getRef().removeValue();
}
}
public void onCancelled(FirebaseError firebaseError) {
}
});
The FirebaseRecyclerAdapter.class
from https://github.com/mmazzarolo/firebase-recyclerview works.
I just copied and pasted, for the most part. This is an abstract class you can use to make a RecyclerView adapter for your Firebase database. All the code is on that GitHub repository for you. Here is a snippet from FirebaseRecyclerAdapter.class
that removes the value from Firebase AND updates the recyclerView:
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
String key = dataSnapshot.getKey();
if (mKeys.contains(key)) {
int index = mKeys.indexOf(key);
T item = mItems.get(index);
mKeys.remove(index);
mItems.remove(index);
notifyItemRemoved(index);
itemRemoved(item, key, index);
}
}
UPDATE: You can hide a view in the Viewholder of the RecyclerView adapter. look at these answers in stackoverflow Hiding views in RecyclerView
The FirebaseUI FirebaseRecyclerAdapter
is a direct representation of the data in the underlying Query
or Firebase
location. To remove an item from the adapter (and view), you have to remove it from the Firebase location (or ensure it doesn't match the query anymore).
So when the user triggers the deletion (e.g. by clicking on an item at a certain position):
adapter.getRef(position).remove()
Also see this issue in the FirebaseUI Github repo.
Now you can delete specific child easy
(1)- First Step create method copy below code:
private void deleteRef(String id) {
databaseRef.child(id).removeValue();
}
(2) - Second step call this method in where you want like alert dialog or firebase recycler adapter in AlertDialog
copy below:
public void alertDelete(final String id) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.warning_delete, null);
dialogBuilder.setView(dialogView);
final AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
final Button deete = dialogView.findViewById(R.id.delete);
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
deleteRef(id);
alertDialog.dismiss();
Toast.makeText(Teachers_Admin_Activity.this,"Delete Successfully",Toast.LENGTH_LONG).show();
}
});
}
(3) Or delete from recyclerview child node like this, but remember must call from ViewHolder
:
@Override
protected void onBindViewHolder(@NonNull final BlogViewHolder holder, final int position, @NonNull final TeachersName model) {
// this is method in viewholer where you next step to delete specific child node //
DatabaseReference itemRef = getRef(position);
final String myKeyItem = itemRef.getKey();
}
(4)- Last step onclicklistener:
holder.deleteImage.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
alertDelete(myKeyItem);
return true;
}
});