Is there a way to sort the firebaserecyclerview af

2019-06-14 15:57发布

I'm using firebase database to store lat and lng of users. In this manner, enter image description here

Now when I retrieve them in my recyclerview I calculate the distance from my current location to that users location using the following code,

private void loadLocationForThisUser(Double lat, Double lng, Double lat1, 
    Double lng1) {


        //Create location from user coordinates
        currentUser1 = new Location("");
        currentUser1.setLatitude(lat);
        currentUser1.setLongitude(lng);

        //Create location from friend coordinates
        friend1 = new Location("");
        friend1.setLatitude(lat1);
        friend1.setLongitude(lng1);

        distance = (new DecimalFormat("#.#").format((currentUser1.distanceTo(friend1)) / 1000)+ "km");
}

and call this method in the following way in my firebase recyclerview adapter,

FirebaseRecyclerOptions<Contacts> options = new FirebaseRecyclerOptions.Builder<Contacts>()
            .setQuery(UsersRef, Contacts.class)
            .build();

    adapter = new FirebaseRecyclerAdapter<Contacts, FindFriendViewHolder>(options) {
        @Override
        protected void onBindViewHolder(@NonNull final FindFriendViewHolder holder, int position, @NonNull final Contacts model) {

            lat = mLastLocation.getLatitude();
            lng = mLastLocation.getLongitude();
            lat1 = Double.parseDouble(model.getMylat());
            lng1 = Double.parseDouble(model.getMylng());                   
            loadLocationForThisUser(lat, lng, lat1, lng1);
            holder.txt_distance.setText(distance);
        }

        @NonNull
        @Override
        public FindFriendViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {

            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.users_display_layout_small, viewGroup, false);
            FindFriendViewHolder viewHolder = new FindFriendViewHolder(view);
            return viewHolder;
        }
    };
    adapter.startListening();
    FindFriendsRecyclerList.setAdapter(adapter);
    adapter.notifyDataSetChanged();
} 

This populates my recyclerview with viewholders with users and their distance i.e. how far they are from me. But the list is plopulated in the order it is stored in the firebase database as a result the list of users is not sorted according to their distance. If user A is farther from me than user B but in the database he's stored above user B then in the output too User A is above User B. The expected output is that user B should come above user A in the recycler view as his distance is lesser compared to that of user A. So accordingly for all users it should check who is closer to me and place them above the ones farther than me, basically sort them based on the distance from me.

1条回答
Deceive 欺骗
2楼-- · 2019-06-14 16:41

The FirebaseUI adapters will display items in the order in which the query returns them. There is no built-in way to reorder items client-side.

You could try the getItem trick shown here: https://stackoverflow.com/a/40383539.

But if that doesn't work for you, you'll have to implement your own adapter: firebaserecycleradapter sort/compare/reorder data before populate.

查看更多
登录 后发表回答