GeoFire Firebase 查询无法将用户 ID 存储在 ArrayList 中

GeoFire Firebase Query not able to Store userID in ArrayList

我正在使用 GeoFire 让所有用户都在一定半径内。当我添加 GeoQueryEventListener & 然后使用 for 循环获取所有用户 ID 时,它没有返回 UserID,而是进入另一个更深的嵌套返回 g 和 1。

private int radius = 40;
private Boolean userFound = false;
private String userLocationID;
private ArrayList<String> mUserIDLocation;
final UserLocation userLocation = new UserLocation();
public void getUserLocation() {

    mUserIDLocation = new ArrayList<String>();

    if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling

        return;
    }
    fusedLocationClient.getLastLocation().addOnSuccessListener((Activity) MainActivity.this, new OnSuccessListener<Location>() {

        @Override
        public void onSuccess(Location location) {

            if (location != null) {
                //Toast.makeText(this, "UserLocation " + location.toString(), Toast.LENGTH_SHORT ).show();
                //final Location userLocation = location;
                Log.d(TAG, "onSuccess: UserLocation" + location);
                Log.d(TAG, "onSuccess: UserLocation Latitude " + location.getLatitude());

                String user_id = FirebaseAuth.getInstance().getCurrentUser().getUid();
                final DatabaseReference mRef = FirebaseDatabase.getInstance().getReference();

                final GeoFire geoFire = new GeoFire(mRef.child("user_location"));
                geoFire.setLocation(user_id, new GeoLocation(location.getLatitude(), location.getLongitude()), new GeoFire.CompletionListener() {
                    @Override
                    public void onComplete(String key, DatabaseError error) {

                    }
                });

                GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(location.getLatitude(), location.getLongitude()), radius);


                geoQuery.addGeoQueryDataEventListener(new GeoQueryDataEventListener() {
                    @Override
                    public void onDataEntered(DataSnapshot dataSnapshot, GeoLocation location) {
                        Log.d(TAG, "onDataEntered: DataSnapshot Key " + dataSnapshot);

                        for(DataSnapshot snapshot : dataSnapshot.getChildren()){
                            mFollowing.add(snapshot.getKey());
                        }

                        getPost();
                        Log.d(TAG, "onDataEntered: mFollowing " + mFollowing);
                        Log.d(TAG, "onDataEntered: mFollowing size " + mFollowing.size());

                    }

                    @Override
                    public void onDataExited(DataSnapshot dataSnapshot) {

                    }

                    @Override
                    public void onDataMoved(DataSnapshot dataSnapshot, GeoLocation location) {

                    }

                    @Override
                    public void onDataChanged(DataSnapshot dataSnapshot, GeoLocation location) {

                    }

                    @Override
                    public void onGeoQueryReady() {

                    }

                    @Override
                    public void onGeoQueryError(DatabaseError error) {

                    }
                });
            }
        }
    });

}

数据库快照日志:

onDataEntered:DataSnapshot Key DataSnapshot { key = xjfXxJ3spuPNywtHyqg5rNnlIMD3,value = {.priority=c295tcm4kd,l={0=48.435,1=-122.084},g=c295tcm4kd}}

onDataEntered:DataSnapshot Key DataSnapshot { key = TMOb5NL8igZovGkiZdVcl3UQmxV2,value = {.priority=c295myvnsd,l={0=48.4319983,1=-122.084},g=c295myvnsd}}

记录添加到 mFollowing ArrayList 的内容: onDataEntered:mFollowing [g,l] onDataEntered:mFollowing [g, l, g, l]

记录 mFollowing ArrayList 的大小: onDataEntered: mFollowing 大小 2 onDataEntered: mFollowing size 4

我的数据库图片: enter image description here

我希望发生的是将 userID 添加到 mFollowing,当我 运行 mFollowing 的日志时,我在数组中只看到 2 个 userID,计数为 2。

我想通了。 OnDataEntered() 是一个检查该区域所有用户的循环。因此,您将所有用户存储在一个 Arraylist 中,然后将它们传递到 OnGeoQueryReady()

中的另一个数组列表

下面是正确的代码。

    private int radius = 40;
    private Boolean userFound = false;
    private String userLocationID;
    private ArrayList<String> mUserIDLocation;
    final UserLocation userLocation = new UserLocation();
    public void getUserLocation() {
        final DatabaseReference mRef = FirebaseDatabase.getInstance().getReference();
        final GeoFire geoFire = new GeoFire(mRef.child("user_location"));
        mUserIDLocation = new ArrayList<String>();

        swipe.setRefreshing(false);

        if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling

            return;
        }
        fusedLocationClient.getLastLocation().addOnSuccessListener((Activity) MainActivity.this, new OnSuccessListener<Location>() {

            @Override
            public void onSuccess(Location location) {

                if (location != null) {
                    //Toast.makeText(this, "UserLocation " + location.toString(), Toast.LENGTH_SHORT ).show();
                    //final Location userLocation = location;
                    Log.d(TAG, "onSuccess: UserLocation" + location);
                    Log.d(TAG, "onSuccess: UserLocation Latitude " + location.getLatitude());

                    String user_id = FirebaseAuth.getInstance().getCurrentUser().getUid();




                    geoFire.setLocation(user_id, new GeoLocation(location.getLatitude(), location.getLongitude()), new GeoFire.CompletionListener() {
                        @Override
                        public void onComplete(String key, DatabaseError error) {

                        }
                    });

                    GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(location.getLatitude(), location.getLongitude()), radius);


                    geoQuery.addGeoQueryDataEventListener(new GeoQueryDataEventListener() {
                        @Override
                        public void onDataEntered(DataSnapshot dataSnapshot, GeoLocation location) {

                            Log.d(TAG, "onDataEntered: datasnapshot " + dataSnapshot);

                            mUserIDLocation.add(dataSnapshot.getKey());


                        }

                        @Override
                        public void onDataExited(DataSnapshot dataSnapshot) {

                        }

                        @Override
                        public void onDataMoved(DataSnapshot dataSnapshot, GeoLocation location) {

                        }

                        @Override
                        public void onDataChanged(DataSnapshot dataSnapshot, GeoLocation location) {

                        }

                        @Override
                        public void onGeoQueryReady() {

                            mFollowing = mUserIDLocation;

                            Log.d(TAG, "onGeoQueryReady: mFollowing users " + mFollowing);

                            getPost();
                        }

                        @Override
                        public void onGeoQueryError(DatabaseError error) {

                        }
                    });
                }
            }
        });

    }

现在,当 mFollowing 传递给 getPost()

时,数组大小将是正确的