How to remove items from firebase RecyclerView

后端 未结 3 621
你的背包
你的背包 2020-12-10 07:42

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

3条回答
  •  时光取名叫无心
    2020-12-10 08:24

    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

提交回复
热议问题