mardi 26 mars 2019

How to display the error when the loop misses a matching record

I have this database iterator that iterates through Firebase database and gets all the values and displays them in a recyclerview. Inside the loop i add an if condition that checks to filter all the values with a certain condition...The positive part works very well.Am wondering how can i get the exception where no items meet that condition.For now nothing happens.

 mUsersDatabaseReference.addValueEventListener(new
ValueEventListener() {
    @Override
    public void onDataChange (DataSnapshot dataSnapshot){
        int count = 0;
        Iterator<DataSnapshot> items = dataSnapshot.getChildren().iterator();
        while (items.hasNext()) {
            DataSnapshot item = items.next();

            //Define the names of the values to be picked
            String theName;
            String theImage;
            String from;
            String to;
            String price;
            String time;
            String id;
            if (item.child("DepartureLatitude").exists() && item.child("DestinationLongitude").getValue() != null) {

                //Define the variables
                theName = item.child("name").getValue().toString();
                from = item.child("DepartureLocation").getValue().toString();
                to = item.child("DestinationLocation").getValue().toString();
                price = item.child("TripPrice").getValue().toString();
                time = item.child("DepartureTime").getValue().toString();
                theImage = item.child("DriverImage").getValue().toString();
                String get = item.child("DriverImage").getRef().getParent().toString();
                id = get.substring(get.lastIndexOf("/") + 1);

                //Driver Depature
                Double deplat = Double.valueOf(item.child("first").getValue().toString());
                Double deplong = Double.valueOf(item.child("second").getValue().toString());
                Double first = deplat * deplong;

                //Driver destination
                Double destlat = Double.valueOf(item.child("DestinationLatitude").getValue().toString());
                Double destlong = Double.valueOf(item.child("DestinationLongitude").getValue().toString());
                Double sec = deplong * deplat;

                if (first <= distance && sec <= distance && item.child("DepartureDay").getValue().toString().equals(name)) {

                    UserDB entry = new UserDB(theName, from, to, price, time, theImage, id);
                    entries.clear();
                    entries.add(entry);
                    mRecyclerview.setAdapter(new RecUsersAdapter(getActivity(), entries));
                    findingLocation.dismiss();
                }
            } else {
                message = "Sorry! Some records are missing";
                String reason = message;
                Toast.makeText(view.getContext(), reason, Toast.LENGTH_SHORT).show();
            }
        }

        mUsersDatabaseReference.removeEventListener(this);
    }

    @Override
    public void onCancelled (DatabaseError databaseError){
        String reason = databaseError.getMessage().toString();
        Toast.makeText(view.getContext(), reason, Toast.LENGTH_SHORT).show();
    }
});

Aucun commentaire:

Enregistrer un commentaire