H.Z.
H.Z.

Reputation: 13

How to Detect if Firebase Listeners finished fetching Data?

I'm using Firebase ValueEventlistener in an Android app to retreive some data from the database to initialize my UI, but my Problem is that I never know when data fetching is done and it depends on the speed of the user's internet connection, I'm making a delay of 5 seconds for the Listeners to finish fetching data but sometimes it's not enough and data is still null giving a null pointer exception. I want to know when the Firebase Listeners finish fetching data to be able to update my UI .

This is one Method where I fetch Data :

      public void GetRequests(){

         Retrieve.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
     Iterable<DataSnapshot> Games = dataSnapshot.getChildren();


            for(com.google.firebase.database.DataSnapshot game : Games ){

                String Game_Key = game.child("GameKey").getValue(String.class);
                String Game_Location = game.child("Location").getValue(String.class);
                String UserID_Sender = game.child("UserID").getValue(String.class);
                Adapter_Game_Key.add(Game_Key);
                Adapter_UserID_Sender.add(UserID_Sender);
           }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

Here I try to get the data from the Firebase Listeners in an ArrayList to be able to initialize the UI by passing the ArrayLists to an Adapter of a ListView.

Anyone has a solution for this please ?

Upvotes: 1

Views: 2997

Answers (1)

Tata Fombar
Tata Fombar

Reputation: 349

Do all the UI updates inside the onDataChanged() method as follows:

    ArrayList<Ad> adArrayList_1 = new ArrayList<>();
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("ads");

    ListView listView_1 = (ListView) findViewById(R.id.market_list_view_1);

    MarketArrayAdapter marketArrayAdapter = new MarketArrayAdapter(
            this,
            adArrayList_1
    );

    listView_1.setAdapter(marketArrayAdapter);

            ref.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    adArrayList_1.clear();
                    for(DataSnapshot snapshot : dataSnapshot.getChildren()){
                        Ad ad = snapshot.getValue(Ad.class);
                        adArrayList_1.add(ad);

                    }
                    marketArrayAdapter.notifyDataSetChanged();
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

My adapter class is below

public MarketArrayAdapter(Context context, ArrayList<Ad> list) {
    super(context, 0, list);
}

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    final Ad ad = getItem(position);
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.ad_item, parent, false);
    }
    ImageView imageView = (ImageView) convertView.findViewById(R.id.ads_profile_pic);
    TextView title = (TextView) convertView.findViewById(R.id.ads_title);
    TextView price = (TextView) convertView.findViewById(R.id.ads_price);
    TextView description = (TextView) convertView.findViewById(R.id.ads_short_description);

    byte[] imageAsBytes = Base64.decode(ad.getaPictureUri().getBytes(), Base64.DEFAULT);
    imageView.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length));
    title.setText(ad.getaTitle());
    price.setText(String.valueOf(ad.getaPrice()));
    description.setText(ad.getaDescription());

    parent.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent in = new Intent(MarketActivity.this, ViewAdActivity.class);
            in.putExtra("key", ad.getaId());
            startActivity(in);
        }
    });
    return convertView;
}

}

Please dont forget to put this line marketArrayAdapter.notifyDataSetChanged();

Upvotes: 4

Related Questions