返回多个arraylist .size()而不是一个大小

时间:2019-04-03 22:31:52

标签: java android firebase arraylist geofire

当使用addGeoQueryDataEventListener时,我将所有用户存储在一个arraylist中(应该为3个),除了我要获取arraylist size()时,其打印的是1,2,3,而不是3。

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

        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(48.435, -122.084), radius);


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

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

                mFollowing.add(dataSnapshot.getKey());

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

                getPost();

            }

            @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) {

            }
        });

    }

一个用户数据快照的日志:

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

mFollowing数组的日志:

2019-04-03 18:13:30.821 28961-28961/? D/MainActivity: onDataEntered: mFollowing [xjfXxJ3spuPNywtHyqg5rNnlIMD3]

2019-04-03 18:13:31.651 28961-28961/com.beepic D/MainActivity: onDataEntered: mFollowing [xjfXxJ3spuPNywtHyqg5rNnlIMD3, TMOb5NL8igZovGkiZdVcl3UQmxV2]

2019-04-03 18:13:31.653 28961-28961/com.beepic D/MainActivity: onDataEntered: mFollowing [xjfXxJ3spuPNywtHyqg5rNnlIMD3, TMOb5NL8igZovGkiZdVcl3UQmxV2, IWJbi7sQe2MdbWET9d1vEqum9tb2]

mFollowing.size()的日志

2019-04-03 18:13:30.821 28961-28961/? D/MainActivity: onDataEntered: mFollowing Size 1
2019-04-03 18:13:31.651 28961-28961/com.beepic D/MainActivity: onDataEntered: mFollowing Size 2
2019-04-03 18:13:31.653 28961-28961/com.beepic D/MainActivity: onDataEntered: mFollowing Size 3

下面是我使用mFollowing.size()从存储在mFollowing中的用户获取所有帖子的地方。

    private void getPost(){
        Log.d(TAG, "getPost: getting post");

        DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
        Log.d(TAG, "getPost: mfollowing size3 " + mFollowing.size());
        for( int i = 0; i < mFollowing.size(); i++){


            Log.d(TAG, "getPost: mFollowing " + mFollowing);
            Log.d(TAG, "getPost: getting size " + mFollowing.size());
            final int count = i;
            Query query = reference
                    .child(getString(R.string.dbname_user_interest_post))
                    .child(mFollowing.get(i))

                    .orderByChild(getString(R.string.field_user_id))
                    .equalTo(mFollowing.get(i));

            Log.d(TAG, "getPost: getPost query: " + query);
            query.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
                        Log.d(TAG, "onDataChange: main feed getChildren " + dataSnapshot.getChildren());
                        Log.d(TAG, "onDataChange: onDataChange main feed: " + singleSnapshot
                                .child(getString(R.string.field_user_id)));

                        NewPost post = new NewPost();
                        Map<String, Object> objectMap = (HashMap<String, Object>) singleSnapshot.getValue();

                        post.setCaption(objectMap.get(getString(R.string.field_caption)).toString());
                        post.setPhoto_id(objectMap.get(getString(R.string.field_photo_id)).toString());
                        post.setUser_id(objectMap.get(getString(R.string.field_user_id)).toString());
                        post.setInterest(objectMap.get(getString(R.string.string_interest)).toString());
                        post.setDate_created(objectMap.get(getString(R.string.field_date_created)).toString());


                        post.setImage_path(objectMap.get(getString(R.string.field_image_path)).toString());

                        Log.d(TAG, "onDataChange: getPost caption main " + post);

                        mNewPost.add(post);
                    }

                    if(count >= mFollowing.size() -1){

                        //display post
                        displayPost();
                    }

                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });
        }

    }

这是在getPost()中使用时的日志mFollowing;

2019-04-03 18:19:32.476 29167-29167/? D/MainActivity: getPost: mFollowing [xjfXxJ3spuPNywtHyqg5rNnlIMD3]
2019-04-03 18:19:33.019 29167-29167/com.beepic D/MainActivity: getPost: mFollowing [xjfXxJ3spuPNywtHyqg5rNnlIMD3, TMOb5NL8igZovGkiZdVcl3UQmxV2]
2019-04-03 18:19:33.020 29167-29167/com.beepic D/MainActivity: getPost: mFollowing [xjfXxJ3spuPNywtHyqg5rNnlIMD3, TMOb5NL8igZovGkiZdVcl3UQmxV2]
2019-04-03 18:19:33.020 29167-29167/com.beepic D/MainActivity: getPost: mFollowing [xjfXxJ3spuPNywtHyqg5rNnlIMD3, TMOb5NL8igZovGkiZdVcl3UQmxV2, IWJbi7sQe2MdbWET9d1vEqum9tb2]
2019-04-03 18:19:33.021 29167-29167/com.beepic D/MainActivity: getPost: mFollowing [xjfXxJ3spuPNywtHyqg5rNnlIMD3, TMOb5NL8igZovGkiZdVcl3UQmxV2, IWJbi7sQe2MdbWET9d1vEqum9tb2]
2019-04-03 18:19:33.021 29167-29167/com.beepic D/MainActivity: getPost: mFollowing [xjfXxJ3spuPNywtHyqg5rNnlIMD3, TMOb5NL8igZovGkiZdVcl3UQmxV2, IWJbi7sQe2MdbWET9d1vEqum9tb2]

正如您所看到的,这导致我的帖子数全部消失。在实现GeoFire之前,我没有使用下面的代码,但它可以正常工作,但它可以使每个用户都进入数据库。 m以下大小为7(数据库中的当前用户数)。

    private void getUsersPost() {
        Log.d(TAG, "getUsersPost: getting all the post");

        swipe.setRefreshing(false);

        Log.d(TAG, "onKeyEntered: setUserLocation insidegetUserPost " +  userLocation.getUserLocations());

        DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
        Query query = reference
                .child("user_location");
        //getString(R.string.dbname_user_interest_post)

        Log.d(TAG, "getUsersPost: getUserPost query main: " + query);
        query.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()) {
                    Log.d(TAG, "onDataChange: found user main getUsersPost: " + singleSnapshot.getKey());

                    mFollowing.add(singleSnapshot.getKey());

                    Log.d(TAG, "onDataEntered: mFollowing1 " + mFollowing);
                    Log.d(TAG, "onDataEntered: mFollowing size1 " + mFollowing.size());
                }
                Log.d(TAG, "onDataEntered: mFollowing2 " + mFollowing);
                Log.d(TAG, "onDataEntered: mFollowing size2 " + mFollowing.size());

                //get the post
                getPost();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });


    }

如果有人有任何疑问,请询问。我很想解决这个问题。

0 个答案:

没有答案