0

The following is my firebase database structure. enter image description here

The following is the method that I get the data from firebase database to the recycler view. However, there is an unique id(red oval) in the child part. How can I go after the unique id and filter the data using the key "category"? Also, I want to order them in acensding order but firebase does not provide a method for that. Where can I get the list of data and reverse them? Please give me some helps. Thank you very much.

    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);
            }
        });
    }
}
ng2b30
  • 351
  • 6
  • 18

1 Answers1

0

When you use the push() method to generate the unique keys, is the moment in which you can get the keys. Please use this code:

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

Because of the unique ids, the items are already ordered by date (ascending order). If you want to order descending you need to use a TIMESTAMP like this: -1 * ServerValue.TIMESTAMP. Here is also the official documentation which will help you do more than that. You will be able to orderByChild(), orderByKey() or orderByValue() and also limitToFirst(), limitToLast(), equalTo() or to use intervals like: startAt() and endAt(). Hope it helps! :)

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • But this means that I have to get the key when I create them? How to do that if I am not the creator? Also, would you mind to give me more further example about the TIMESTAMP like this: -1 * ServerValue.TIMESTAMP? Thank you very much. – ng2b30 Mar 31 '17 at 15:21
  • Than you need to put a `listener` on the `Product` node, iterate over all childrens, get the id's like this: `String pushedKey = this.getRef(position).getKey();` than do wahtever you want with them. Regarding `-1 * ServerValue.TIMESTAMP`, you first need to set a `TIMESTAMP` for each product. `-1 *` creates the negative `TIMESTAMP`. So becsuse the default order is ascending, the `-1` makes the order to descending. – Alex Mamo Mar 31 '17 at 15:37
  • Is there any way for me to get them first and then take the further actions? – ng2b30 Mar 31 '17 at 16:23
  • Regardind your first question, please take o look at this [post](http://stackoverflow.com/questions/41281855/how-to-read-childrens-from-a-firebase-database-using-firebaselistadapter-in-java). And for the second, please take a look at [this](http://stackoverflow.com/questions/34156996/firebase-data-desc-sorting-in-android) at Frank van Puffelen answer. Hope you can solve the problems. – Alex Mamo Apr 01 '17 at 11:16
  • I change the methods to get the whole data by using addValueEventListener and sort them. Then I put them in the recycler adapter instead of using the firebaserecycleradapter. But I dun know that my way is good or not. Btw, Thank you for your help. – ng2b30 Apr 01 '17 at 13:15