以下载的降序显示项目 [英] Display items in descending order of downloads

查看:50
本文介绍了以下载的降序显示项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我正在将FirebaseUI用于android和Firebase数据库.

First of all, I'm using FirebaseUI for android and Firebase Database.

由于firebase查询仅支持按升序对数据进行排序,因此我想出了一个小技巧,可以按降序在 RecyclerView 中显示项目.

Since firebase querying only supports ordering of data in ascending order so I came up with a little hack to display items in RecyclerView in descending order.

这是我的查询方式.

selectedCategory = "downloads"
array = new FirebaseArray(imageRef.orderByChild(selectedCategory).limitToLast(50), ImageModel.class);

这是我在 FirebaseRecyclerAdapter 上的实现.请注意,这里我覆盖了 onBindViewHolder 方法.这是重新排序项目的方法.但是实际上它不能按预期方式工作(使用或不使用此代码均无效.两种情况下的列表相同).

Here's my implementation on FirebaseRecyclerAdapter. Please, note that here I override the method onBindViewHolder. This is the method which works to reorder items. But practically it doesn't work as expected (no effect with or without this code. List is same in both cases).

FirebaseRecyclerAdapter<ImageModel, GridViewHolder>(
            array,
            R.layout.grid_item,
            GridViewHolder.class) {

        @Override
        public void onBindViewHolder(GridViewHolder viewHolder, int position) {
            super.onBindViewHolder(viewHolder, getItemCount() -position - 1);
            populateViewHolder(viewHolder, getItem(position), getItemCount() - position - 1);
        }

        @Override
        protected void populateViewHolder(final GridViewHolder viewHolder, final ImageModel model, final int position) {
        .
        .
        .

推荐答案

如果我理解正确,则希望基于下载次数"进行排序.为什么不存储值

If I understand correctly, you want to sort based on the "downloads" count. Why don't you store the value

download * (-1)

0 - download

这样,您将在数据库中获得所需的顺序,然后无需在Android端工作.

This way you will get the desired order in the database and then no more need to work on the Android side.

这篇关于以下载的降序显示项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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