列表未添加位置类型的值

时间:2018-03-01 09:14:07

标签: android arraylist firebase-realtime-database geofire

我正在处理地点。我使用geoFire.getLocation(riderID, new LocationCallback()从firebase获取位置。我在onCreate方法上创建了rLocations = new ArrayList<>();。然后我在`public void passingRiderID(String riderID)中添加值。

public void findingNearestRider()我希望找到最近的炊具,但我发现列表是空的。

 DatabaseReference riderPRIMARYKEY = FirebaseDatabase.getInstance().getReference("Rider Primary Key");
DatabaseReference riderLocation = FirebaseDatabase.getInstance().getReference("Rider Location Information");

FirebaseUser cookerRefrence = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference cookerLocationRefrence = FirebaseDatabase.getInstance().getReference("Expert Location Information");

GeoFire geoFireCooker= new GeoFire(cookerLocationRefrence);

GeoFire geoFire = new GeoFire(riderLocation);

public  static  List<Location> rLocations;

Location cookerLOCATION;

public static Location colsestRiderLocation;
public  static float smallestDistance = -1;


TextView riderID;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_riders_ids2);
    riderID =(TextView)findViewById(R.id.tvRiderID);

    rLocations = new ArrayList<>();


    try{
        findingRiderID();
    }catch (Exception e){
        e.printStackTrace();
    }finally {
        findingCookerLocation();

    }
}
public void findingRiderID(){
    riderPRIMARYKEY.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot riderSnapshot: dataSnapshot.getChildren()){
                try{
                    Log.d("User key", riderSnapshot.getKey());
                    passingRiderID(riderSnapshot.getKey());
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}
public void passingRiderID(String riderID){

    geoFire.getLocation(riderID, new LocationCallback() {
        @Override
        public void onLocationResult(String key, GeoLocation riderID) {


            Location riderLOCATION = new Location("");
            riderLOCATION.setLatitude(riderID.latitude);
            riderLOCATION.setLongitude(riderID.longitude);

            Log.d("Rider location", riderLOCATION.toString());


            rLocations.add(riderLOCATION); //here adding values into list

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}
public void findingCookerLocation(){
    geoFireCooker.getLocation(cookerRefrence.getUid(), new LocationCallback() {
        @Override
        public void onLocationResult(String key, GeoLocation cookerID) {

            cookerLOCATION = new Location("");
            cookerLOCATION.setLatitude(cookerID.latitude);
            cookerLOCATION.setLongitude(cookerID.longitude);

            Log.d("cooker location",cookerLOCATION.toString());
            findingNearestRider();

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}
public void findingNearestRider(){

    if(rLocations.isEmpty()){
        Log.d("Closet Rider  empty",rLocations.toString());
    }else{
        try {
            for(Location rlocation : rLocations){


                float distance=  cookerLOCATION.distanceTo(rlocation);
                //int distance = Location.distanceBetween(cookLAT,cookLONG,rlocation.getLatitude(),rlocation.getLongitude(),results);
                if(smallestDistance == -1 || distance < smallestDistance){
                    colsestRiderLocation = rlocation;
                    smallestDistance = distance;
                    Log.d("Closet Rider Location",colsestRiderLocation.toString());
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }



}

我按你说的方式更改了代码。

DatabaseReference riderPRIMARYKEY = FirebaseDatabase.getInstance().getReference("Rider Primary Key");
DatabaseReference riderLocation = FirebaseDatabase.getInstance().getReference("Rider Location Information");

FirebaseUser cookerRefrence = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference cookerLocationRefrence = FirebaseDatabase.getInstance().getReference("Expert Location Information");

GeoFire geoFireCooker= new GeoFire(cookerLocationRefrence);

GeoFire geoFire = new GeoFire(riderLocation);

public  List<Location> rLocations;

Location cookerLOCATION;
Location riderLOCATION;

public static Location colsestRiderLocation;
public  static float smallestDistance = -1;


TextView riderID;

Boolean checkCook=false,checkRider=false;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_riders_ids2);
    riderID =(TextView)findViewById(R.id.tvRiderID);

    rLocations = new ArrayList<>();


    try{

        findingRiderID(new passignriderID() {
            @Override
            public void onCallback(String riderID) {
                Log.d("Rider Id", riderID);

                passingRiderID(riderID, new storingriderID() {
                    @Override
                    public void onCallback(Location riderLoc) {
                        Log.d("Rider location", riderLoc.toString());
                        rLocations.add(riderLoc); //now here adding values
                        checkRider=true;
                    }
                });
            }
        });
        findingCookerLocation(new cookerID() {
            @Override
            public void onCallback(Location cookerLoc) {
                cookerLOCATION = cookerLoc;
                checkCook=true;
                Log.d("cooker location",cookerLOCATION.toString());
            }
        });
    }catch (Exception e){
        e.printStackTrace();
    }finally{
        if(checkRider==true || checkCook== true){
            Log.d("inside check con",cookerLOCATION.toString());

        }
        findingNearestRider(cookerLOCATION,rLocations);
    }




}
public void findingRiderID(final passignriderID myCallback){
    riderPRIMARYKEY.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot riderSnapshot: dataSnapshot.getChildren()){
                try{
                    //Log.d("User key", riderSnapshot.getKey());
                    String key = riderSnapshot.getKey();
                    myCallback.onCallback(key);

                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}
public void passingRiderID(String riderID, final storingriderID myCallback){

    geoFire.getLocation(riderID, new LocationCallback() {
        @Override
        public void onLocationResult(String key, GeoLocation riderID) {


            riderLOCATION = new Location("");
            riderLOCATION.setLatitude(riderID.latitude);
            riderLOCATION.setLongitude(riderID.longitude);

            //Log.d("Rider location", riderLOCATION.toString());
            myCallback.onCallback(riderLOCATION);



        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}
public void findingCookerLocation(final cookerID myCallback){
    geoFireCooker.getLocation(cookerRefrence.getUid(), new LocationCallback() {
        @Override
        public void onLocationResult(String key, GeoLocation cookerID) {

            cookerLOCATION = new Location("");
            cookerLOCATION.setLatitude(cookerID.latitude);
            cookerLOCATION.setLongitude(cookerID.longitude);
            myCallback.onCallback(cookerLOCATION);
            //Log.d("cooker location",cookerLOCATION.toString());


        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}
public void findingNearestRider(Location cookerLoc, List<Location> rLoc){

    /*if(rLoc.isEmpty()){
        Log.d("Closet Rider  empty",rLoc.toString());
    }else{


        for(Location loc : rLoc){
            Log.d("Closet Rider not empty", String.valueOf(loc));
            Log.d("cooker loc in find", String.valueOf(cookerLoc));
        }
    }*/

    if(rLoc.isEmpty()){
        Log.d("Closet Rider  empty",rLoc.toString());
    }else{
        try {
            for(Location rlocation : rLoc){


                float distance=  cookerLoc.distanceTo(rlocation);
                //int distance = Location.distanceBetween(cookLAT,cookLONG,rlocation.getLatitude(),rlocation.getLongitude(),results);
                if(smallestDistance == -1 || distance < smallestDistance){
                    colsestRiderLocation = rlocation;
                    smallestDistance = distance;
                    Log.d("Closet Rider Location",colsestRiderLocation.toString());
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
public interface passignriderID{
    void onCallback(String riderID);
}
public interface storingriderID{
    void onCallback(Location riderLoc);
}
public interface cookerID{
    void onCallback(Location cookerLoc);
}

这是解决方案。

DatabaseReference riderPRIMARYKEY = FirebaseDatabase.getInstance().getReference("Rider Primary Key");
DatabaseReference riderLocation = FirebaseDatabase.getInstance().getReference("Rider Location Information");

FirebaseUser cookerRefrence = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference cookerLocationRefrence = FirebaseDatabase.getInstance().getReference("Expert Location Information");

GeoFire geoFireCooker= new GeoFire(cookerLocationRefrence);

GeoFire geoFire = new GeoFire(riderLocation);

public  static  List<Location> rLocations;

Location cookerLOCATION;
Location riderLOCATION;

public static Location colsestRiderLocation;
public  static float smallestDistance = -1;


TextView riderID;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_riders_ids2);
    riderID =(TextView)findViewById(R.id.tvRiderID);

    rLocations = new ArrayList<>();


    try{
        findingRiderID(new passignriderID() {
            @Override
            public void onCallback(String riderID) {
                passingRiderID(riderID, new listData() {
                    @Override
                    public void onCallBack(List<Location> list) {
                        Log.d("List data",list.toString());
                        findingNearestRider();
                    }
                });
            }
        });
        findingCookerLocation();
    }catch (Exception e){
        e.printStackTrace();
    }
}
public void findingRiderID(final passignriderID myCallBack){
    riderPRIMARYKEY.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot riderSnapshot: dataSnapshot.getChildren()){
                try{
                    Log.d("User key", riderSnapshot.getKey());
                    String riderID= riderSnapshot.getKey();
                    myCallBack.onCallback(riderID);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}
public void passingRiderID(String riderID, final listData myCallBack){

    geoFire.getLocation(riderID, new LocationCallback() {
        @Override
        public void onLocationResult(String key, GeoLocation riderID) {


            riderLOCATION = new Location("");
            riderLOCATION.setLatitude(riderID.latitude);
            riderLOCATION.setLongitude(riderID.longitude);
            rLocations.add(riderLOCATION);
            Log.d("Rider location", riderLOCATION.toString());

            myCallBack.onCallBack(rLocations);




        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });


}
public void findingCookerLocation(){
    geoFireCooker.getLocation(cookerRefrence.getUid(), new LocationCallback() {
        @Override
        public void onLocationResult(String key, GeoLocation cookerID) {

            cookerLOCATION = new Location("");
            cookerLOCATION.setLatitude(cookerID.latitude);
            cookerLOCATION.setLongitude(cookerID.longitude);

            Log.d("cooker location",cookerLOCATION.toString());


        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}
public void findingNearestRider(){

    /*if(rLocations.isEmpty()){
        Log.d("Closet Rider  empty",rLocations.toString());
    }else{


        for(Location loc : rLocations){
            Log.d("Closet Rider not empty", String.valueOf(loc));
        }
    }*/

    if(rLocations.isEmpty()){
        Log.d("Closet Rider  empty",rLocations.toString());
    }else{
        try {
            for(Location rlocation : rLocations){


                float distance=  cookerLOCATION.distanceTo(rlocation);
                //int distance = Location.distanceBetween(cookLAT,cookLONG,rlocation.getLatitude(),rlocation.getLongitude(),results);
                if(smallestDistance == -1 || distance < smallestDistance){
                    colsestRiderLocation = rlocation;
                    smallestDistance = distance;
                    Log.d("Closet Rider Location",colsestRiderLocation.toString());
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }



}
public interface passignriderID{
    void onCallback(String riderID);
}
private interface listData{
    void onCallBack(List<Location> list);
}

0 个答案:

没有答案