在android中过滤并订购firebase数据

时间:2017-03-31 14:26:38

标签: android firebase firebase-realtime-database

以下是我的firebase数据库结构。 enter image description here

以下是我从firebase数据库获取数据到回收站视图的方法。但是,子部分中有一个唯一的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);
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

使用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()或使用以下间隔:{{1 }和startAt()。 希望能帮助到你! :)