在检索数据后计算值后,是否可以对firebaserecyclerview进行排序? [英] Is there a way to sort the firebaserecyclerview after I calculate value after retrieving data?

查看:86
本文介绍了在检索数据后计算值后,是否可以对firebaserecyclerview进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase数据库存储用户的经纬度.以这种方式,

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

现在,当我在recyclerview中检索它们时,我会使用以下代码来计算从当前位置到该用户位置的距离,

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");
}

,然后在我的firebase recyclerview适配器中以以下方式调用此方法,

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

这会在我的recyclerview中填充具有用户及其距离(即他们与我的距离)的观看者.但是按照该列表在Firebase数据库中存储的顺序填充了列表,结果导致该列表未根据用户的距离进行排序.如果用户A比用户B更远离我,但在数据库中他存储在用户B上方,则在输出中用户A也在用户B上方.预期的输出是用户B在回收者视图中应高于用户A,因为他的距离与用户A相比要小一些.因此,对于所有用户,应该检查谁更靠近我,并将他们放在比我更远的地方,基本上根据与我的距离对他们进行排序.

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.

推荐答案

FirebaseUI适配器将以查询返回它们的顺序显示项目.没有内置的方法可以在客户端对商品进行重新排序.

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.

您可以尝试以下显示的getItem技巧: https://stackoverflow.com/a/40383539 .

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.

这篇关于在检索数据后计算值后,是否可以对firebaserecyclerview进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆