0

I am adding item to recycler view using firebase with MVVM. Problem i am facing is when the child is added in Firebase then they are updated to recycler view and there is change in UI, but when i remove item from firebase but there is no change in UI, the deleted is removed from Recycler view. So how can i update adapter when OnChildRemoved is called in Repo class?

Thanks

HomeFragment class

HomeFragment extends Fragment {


RecyclerView RecyclerView;
public ShowMemeAdapter memeAdapter;

private HomeFragmentViewModel homeFragmentViewModel;

public HomeFragment() {
    // Required empty public constructor
}


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View root = inflater.inflate(R.layout.fragment_home, container, false);




 RecyclerView = root.findViewById(R.id.recyclerViewMeme);

    return root;

}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);


    homeFragmentViewModel = new ViewModelProvider(requireActivity()).get(HomeFragmentViewModel.class);

    homeFragmentViewModel.init();
    memeAdapter = new ShowMemeAdapter(requireActivity(),homeFragmentViewModel.getMemeData().getValue());


    homeFragmentViewModel.getMemeData().observe(getViewLifecycleOwner(), new Observer<ArrayList<MemeData>>() {
        @Override
        public void onChanged(ArrayList<MemeData> memeData) {

            RecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
            RecyclerView.setAdapter(memeAdapter);
            memeAdapter.loadData(memeData);
            memeAdapter.notifyDataSetChanged();

        }
    });

}

Repository Class

public class Repo  {
static Repo instance;
private ArrayList<MemeData> memeDataLs = new ArrayList<>();
private MutableLiveData<ArrayList<MemeData>> memeData = new MutableLiveData<>();
ChildEventListener childEventListener;


DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Data/XXX/");


public static Repo getInstance(){
    if(instance==null)
        instance = new Repo();

     return instance;
}
public MutableLiveData<ArrayList<MemeData>> getData(){


   childEventListener =  databaseReference.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull   DataSnapshot snapshot,  String previousChildName) {
            MemeData memeDetails = snapshot.getValue(MemeData.class);
                memeDataLs.add(0,memeDetails);
                memeData.setValue(memeDataLs);


        }

        @Override
        public void onChildChanged(@NonNull   DataSnapshot snapshot, String previousChildName) {

        }

        @Override
        public void onChildRemoved(@NonNull   DataSnapshot snapshot) {

        }

        @Override
        public void onChildMoved(@NonNull   DataSnapshot snapshot, String previousChildName) {

        }

        @Override
        public void onCancelled(@NonNull   DatabaseError error) {

        }
    });

    return memeData;
}

ViewModel Class

public class HomeFragmentViewModel extends ViewModel implements LifecycleObserver {

private MutableLiveData<ArrayList<MemeData>> memeData;

public Repo repo;


public void init() {

    if(memeData!=null){
        return;
    }
    repo = new Repo();
}

public HomeFragmentViewModel(){

        memeData = Repo.getInstance().getData();
}
public LiveData<ArrayList<MemeData>> getMemeData() {
    return memeData;
}
  • I think that this article, [How to delete a record from Firestore on a RecylerView left/right swipe?](https://medium.com/firebase-tips-tricks/how-to-delete-a-record-from-firestore-on-a-recylerview-left-right-swipe-d65d993f0baf) or [How to delete multiple records from Firestore using RecyclerView multi-selection?](https://medium.com/firebase-tips-tricks/how-to-delete-multiple-records-from-firestore-using-recyclerview-multi-selection-96108e4c6166) might help. – Alex Mamo Jul 12 '21 at 10:33
  • Most likely the problem in your code list is the fact that you left onChildRemoved empty. So refresh the adapter once you remove an item. – Alex Mamo Jul 12 '21 at 10:35
  • @AlexMamo that's where i am facing problem to refresh adapter when item is removed. Don't know what to do in onChildRemoved that's why its empty. – Pranav Panwar Jul 12 '21 at 10:49
  • Simply use [notifyItemRemoved(position)](https://stackoverflow.com/questions/28189371/using-notifyitemremoved-or-notifydatasetchanged-with-recyclerview-in-android). – Alex Mamo Jul 12 '21 at 11:13
  • @AlexMamo Item are removed from Database by admin from database not by user. So if the item is not removed by user itself then how will i get position of item removed. And where to place notifyitemRemoved(position) in adapter(onBindViewHolder) or in fragment? – Pranav Panwar Jul 12 '21 at 11:41
  • In that case, you should listen for real-time changes, and once an item is removed by the admin, remove it from your adapter / view as well. – Alex Mamo Jul 12 '21 at 11:49
  • @AlexMamo That's what i am asking how can i remove that item from adapter/view using MVVM. Because i have done this before while i was not using MVVM, what i did was i notfied adapter in onChildRemoved, but in MVVM i don't know how to do that. what code i have to write for that. – Pranav Panwar Jul 12 '21 at 12:05
  • @AlexMamo I tried to remove item following method in onChildRemoved but for that i have to restart my to see changes while on other hand when data is added in firebase by admin it can be seen while app is running but when item is removed i have to restart app memeDetails = snapshot.getValue(MemeData.class); memeDataLs.remove(memeDetails); memeData.setValue(memeDataLs); method using in on child removed – Pranav Panwar Jul 12 '21 at 17:58

0 Answers0