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

时间:2019-04-02 22:14:13

标签: java android firebase geofire

我正在使用GeoFire来获取具有一定半径的所有用户。当我使用addGeoQueryEventListener并使用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密钥DataSnapshot {密钥= xjfXxJ3spuPNywtHyqg5rNnlIMD3,值= {.priority = c295tcm4kd,l = {0 = 48.435,1 = -122.084},g = c295tcm4kd}}

onDataEntered:DataSnapshot密钥DataSnapshot {密钥= TMOb5NL8igZovGkiZdVcl3UQmxV2,值= {.priority = c295myvnsd,l = {0 = 48.4319983,1 = -122.084},g = c295myvnsd}}

登录到m跟在ArrayList之后: onDataEntered:m跟随[g,l] onDataEntered:m跟随[g,l,g,l]

记录mFollowing ArrayList的大小: onDataEntered:m跟随大小2 onDataEntered:m遵循大小4

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

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

1 个答案:

答案 0 :(得分:0)

我找出了问题所在。 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()时,数组大小将是正确的