当循环错过匹配记录时如何显示错误

时间:2019-03-26 07:35:07

标签: java android if-statement while-loop iterator

我有这个数据库迭代器,它遍历Firebase数据库并获取所有值并将它们显示在recyclerview中。在循环中,我添加了一个if条件,该条件检查以特定条件过滤所有值...正数部分工作得很好。我想知道如何在没有任何项目满足该条件的情况下获得异常。 / p>

 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();
    }
});

1 个答案:

答案 0 :(得分:0)

您有两个条件正在过滤您的值?
@edit,您需要将其置于内部if条件中 试试这个

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();
              }
      } 
    }


会起作用的