Search code examples
androidfirebasefirebase-realtime-databaseandroid-recyclerviewfragmentstatepageradapter

how to retrieve the firebase database values only once without listening to childvalues even though they have changed


I have this scenario where in my app i am trying to query the child nodes and pass it on the list to recyclerview adapter and here comes the problem when i am scrolling up the recycler view items and if some one has inserted a post, my recyclerview is again coming to first post item and also i am using the viewpager with three fragments and whatever fragment i am on I am rolling back to the first fragment if some one has inserted the post how to solve this. I have implemented this in following way mentioned below.

one way im thinking is i thought i would not listen to the childevent changes instead i would query the results and populate recyclerview later not listening to child events so that way everything states as it is and i dont know in firebase how do you retrieve values without implementing listeners I tried the singleValueEventListener that way still the behavior is same rolling back to first item or first fragment

guide me through solution how to get rid of this behavior.

Query query= databasePostsReference.orderByChild("timestamp");

       query.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                List<UserPostPOJO> listposts = new ArrayList<UserPostPOJO>();
                for(DataSnapshot snapshot : dataSnapshot.getChildren()){
                    Log.d(TAG, "onDataChange: entered list adding");
                    UserPostPOJO post = 
                    snapshot.getValue(UserPostPOJO.class);
                    listposts.add(0,post);
                }
                if(listposts.isEmpty()){
                    empty.setVisibility(View.VISIBLE);
                    recyclerView.setVisibility(View.GONE);
                }
                else
                {
                    empty.setVisibility(View.GONE);
                    recyclerView.setVisibility(View.VISIBLE);
                    makelist(listposts);
                }


            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        }); 




private void makelist(List<UserPostPOJO> listposts) {
        list = listposts;
        Log.d(TAG,"size is "+ list.size()+"");

        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new 
        LinearLayoutManager(getActivity()));
        CustomRecyclerViewAdapter adapter = new 
        CustomRecyclerViewAdapter(getActivity(), list,"recentfragment");
        recyclerView.setAdapter(adapter);


    }

Solution

  • There is a method called removeEventListener() that you can call to remove a specific event listener. You get data out from your database and than call this method. So in order to make this work, please use the following code:

    databaseReference.removeEventListener(valueEventListener);
    

    In which databaseReference is the reference where you intially put the listener.

    For more details please read the offcial doc.

    Hope it helps.