【问题标题】:Filter and Order the firebase data in android在android中过滤和排序firebase数据
【发布时间】:2017-03-31 23:03:07
【问题描述】:

以下是我的 firebase 数据库结构。

以下是我从firebase数据库获取数据到recycler视图的方法。但是,子部分中有一个唯一的 id(红色椭圆形)。如何获取唯一 ID 并使用键“类别”过滤数据?另外,我想按升序对它们进行排序,但 firebase 没有为此提供方法。我在哪里可以获得数据列表并反转它们?请给我一些帮助。非常感谢。

    mFilterDatabse = mDatabase.orderByChild("category").equalTo(categoryResult);

    FirebaseRecyclerAdapter<Product, ProductViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Product, ProductViewHolder>(

            Product.class,
            R.layout.product_row,
            ProductViewHolder.class,
            mFilterDatabse
    ) {
        @Override
        protected void populateViewHolder(ProductViewHolder viewHolder, final Product model, int position) {

            Log.d(TAG, "loading view " + position);
            final String product_id = getRef(position).getKey();
            viewHolder.setProductName(model.getProductName());
            viewHolder.setDescription(model.getDescription());
            viewHolder.setImage(getApplicationContext(), model.getProductImage());
            viewHolder.setUid(model.getUid());

            viewHolder.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent productDetailIntent = new Intent();
                    productDetailIntent.setClass(MainActivity.this, ProductDetailActivity.class);
                    productDetailIntent.putExtra("product_id", product_id);
                    Log.d(TAG + " product_id", product_id);
                    productDetailIntent.putExtra("colorNo", model.getColorNo());
                    Log.d(TAG + " colorNo", model.getColorNo() + "");
                    startActivity(productDetailIntent);
                }
            });

            Log.d(TAG, "finish loading view");
        }
    };

    mProductList.setAdapter(firebaseRecyclerAdapter);
    firebaseRecyclerAdapter.notifyDataSetChanged();


public static class ProductViewHolder extends RecyclerView.ViewHolder {

    View mView;
    private Typeface customTypeface = Typeface.createFromAsset(itemView.getContext().getAssets(), FontManager.APP_FONT);

    public ProductViewHolder(View itemView) {
        super(itemView);
        mView = itemView;
    }

    public void setProductName(String productName) {
        TextView product_title = (TextView) mView.findViewById(R.id.p_title);
        product_title.setText(productName);
        product_title.setTypeface(customTypeface);
    }

    public void setDescription(String description) {
        TextView product_desc = (TextView) mView.findViewById(R.id.p_desc);
        product_desc.setText(description);
        product_desc.setTypeface(customTypeface);
    }

    public void setUid(String uid) {
        TextView product_username = (TextView) mView.findViewById(R.id.p_username);
        product_username.setText(uid);
        product_username.setTypeface(customTypeface);
    }

    public void setImage(final Context ctx, final String image) {
        final ImageView post_image = (ImageView) mView.findViewById(R.id.product_image);
        Picasso.with(ctx).load(image).networkPolicy(NetworkPolicy.OFFLINE).into(post_image, new Callback() {
            @Override
            public void onSuccess() {
                Log.d(TAG, "image loading success !");
            }

            @Override
            public void onError() {
                Log.d(TAG, "image loading error !");
                Picasso.with(ctx)
                        .load(image)
                        .resize(100, 100)
                        .centerCrop()
                        .into(post_image);
            }
        });
    }
}

【问题讨论】:

    标签: android firebase firebase-realtime-database


    【解决方案1】:

    当您使用push() 方法生成唯一密钥时,就是您可以获得密钥的时刻。请使用此代码:

    DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child(Product).push();
    String pushedKey = ref.getKey(); // We can do whatever you want with them
    

    由于唯一的 ID,项目已经按日期排序(升序)。如果您想按降序排序,您需要使用TIMESTAMP,如下所示:-1 * ServerValue.TIMESTAMP。这也是官方的documentation,它将帮助您做更多的事情。您将能够orderByChild()orderByKey()orderByValue() 以及limitToFirst()limitToLast()equalTo() 或使用如下间隔:startAt()endAt()。 希望能帮助到你! :)

    【讨论】:

    • 但这意味着我必须在创建它们时获取密钥?如果我不是创作者怎么办?另外,您是否介意给我更多关于 TIMESTAMP 的示例:-1 * ServerValue.TIMESTAMP?非常感谢。
    • 你需要在Product 节点上放置一个listener,遍历所有子节点,得到这样的ID:String pushedKey = this.getRef(position).getKey();,而不是你想要的。关于-1 * ServerValue.TIMESTAMP,首先需要为每个产品设置一个TIMESTAMP-1 * 创建负数 TIMESTAMP。所以因为默认顺序是升序,-1 使顺序降序。
    • 有什么办法让我先得到它们,然后再采取进一步的行动?
    • 关于您的第一个问题,请看post。对于第二个,请查看 Frank van Puffelen 回答中的this。希望你能解决问题。
    • 我更改了使用 addValueEventListener 获取整个数据的方法并对它们进行排序。然后我把它们放在回收器适配器中,而不是使用 firebaserecycleradapter。但我不知道我的方式好不好。顺便说一句,谢谢你的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-21
    • 2014-08-08
    • 1970-01-01
    • 2015-03-21
    • 2019-01-09
    • 1970-01-01
    • 2011-10-09
    相关资源
    最近更新 更多