-1

notifyDataSetChanged() is not Working inside listeners

I have a child event listener that loads the data from the Fire-base and i am loading the data into an array list. I need to set the adapter when data completely loaded. So i used single value event to identify the child event completion and when i try to set the adapter in the single value event listener, it is not setting the adapter. Please Assist..

Below is my code:

Note: I have set my adapter in the oncreate method.

  db.addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            bigProgressBar.setVisibility(View.GONE);
            Log.d("value",abc.toString());// returns empty!!
            sampleAdapter.notifyDataSetChanged();
        }

        }


    db.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(final DataSnapshot dataSnapshot, String s) {
            if (dataSnapshot != null) {

                final Handler handler = new Handler();

                handler.post(new Runnable() {
                    @Override
                    public void run() {


                        Events job = dataSnapshot.getValue(Events.class);

                        abc.add( job);





                    }
                });

            }

        }
  • Try using `.addValueEventListener()` instead and initialize it after the `addChildEventListener()`. Because I used it like that and got what I wanted. Additional note: (1) `dataSnapshot` [will never be null](http://stackoverflow.com/q/41512738/4112725), you should use `dataSnapshot.exist()`. (2) Why you use handler? is there any particular reason? – koceeng Mar 27 '17 at 05:41
  • Thank you @koceeng, I really doesn't want the listener to be called always. I want it to be called only once. Is there any possible way to set the adapter in the singlevalueevent listener. 1) Thanks for you comments.I will edit my code. 2) I have few more codes inside that which i have not mentioned:) – Gokul Raj Sgr Mar 27 '17 at 05:48

1 Answers1

0

I think the reason that notifyDataSetChanged() is not working because it is called before abc.add( job) so the adapter is update before the data is added into your ArrayList.

If you are using RecyclerView, I recommended you to checkout this library. https://github.com/firebase/FirebaseUI-Android

or

To update adapter after loading data to all ArrayList.

db.addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            bigProgressBar.setVisibility(View.GONE);
            for(DataSnapshot child : dataSnapshot.getChildren()) {
                Events event = child.getValue(Events.class);
                abc.add(job);
            }
            sampleAdapter.notifyDataSetChanged();
        }
}

Note that db is the root node contain you Event data.

Boonya Kitpitak
  • 3,607
  • 1
  • 30
  • 30
  • Thank You so much Boonya Kitpitak.It makes a perfect sense, but i am not updating the adapter after added to Firebase, instead i am updating the adapter after all the data loaded into the ArrayList(abc) from Firebase using child event listener. My whole point is, i am retrieving data from the Firebase using child listener and since value event listener is called after the child listener, I am updating the adapter with the loaded arraylist. – Gokul Raj Sgr Mar 27 '17 at 06:36
  • @ Boonya, I have already loaded the data from Firebase to abc, then why we need to add the data again to abc in single value event listener. Instead I am going to set the adapter in the single value event listener. Please Assist me in this area – Gokul Raj Sgr Mar 27 '17 at 09:29
  • Then where did you add the data to `abc` please also include the code. `onChildAdded` is not where you should please the add to `abc` code. – Boonya Kitpitak Mar 27 '17 at 10:28
  • I have included in the code, that I have added the data inside child event listener. Events job = dataSnapshot.getValue(Events.class); abc.add( job); – Gokul Raj Sgr Mar 27 '17 at 10:40
  • According to what you include in your answer that's not where you should perform adding. please replace all of that code with the code that i've given. Or check to document to see what function really perform. – Boonya Kitpitak Mar 27 '17 at 10:55
  • 1
    Thank You so much Boonya. Let me correct the code if I am wrong, But it works elegant for me as of now. Thanks for your patience:) – Gokul Raj Sgr Mar 27 '17 at 11:03