0

i have a list of child in my firebase and key name is dates now i want to order these items in way so that i can latest date item first and also i am using a custom adopter to display this data using FirebaseRecyclerAdapter and i also need to get child key when i click on item: This is hwo my child looks like view here

and this is my code for displaying data

  DatabaseReference personsRef = FirebaseDatabase.getInstance().getReference().child("winners");
        Query personsQuery = personsRef.orderByKey();

        mPeopleRV.hasFixedSize();


        LinearLayoutManager mLayoutManager = new LinearLayoutManager(Notification.this);
        mLayoutManager.setReverseLayout(true);
        mLayoutManager.setStackFromEnd(true);

        mPeopleRV.setLayoutManager(mLayoutManager);

        FirebaseRecyclerOptions personsOptions = new FirebaseRecyclerOptions.Builder<News>().setQuery(personsQuery, News.class).build();

        mPeopleRVAdapter = new FirebaseRecyclerAdapter<News, NewsViewHolder>(personsOptions) {
            @Override
            protected void onBindViewHolder(Notification.NewsViewHolder holder, final int position, final News model) {


                holder.setTitle(capitalize(model.getName()));
                holder.setDesc(model.getDate().replace('-', ' '));
                holder.setAmount(model.getAmount());
                holder.setImage(getBaseContext(), model.getImg());


                holder.mView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                       //Here i need child key when click on item (Date)


                    }
                });
            }

            @Override
            public Notification.NewsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.news_row, parent, false);

                return new Notification.NewsViewHolder(view);
            }
        };

        mPeopleRV.setAdapter(mPeopleRVAdapter);

Things i need:

  1. Order items by date (latest date on first postion in this example
    02-Sep-2018)

  2. get key when user click on items (key i.e. date )

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
sam999
  • 115
  • 1
  • 13

1 Answers1

0

There is no way to sort your items by date unless you make some changes in your database schema. Even if your keys hold some king of "date" (02-Sep-2018) there is not way to order them by date because all Firebase keys are Strings and when the strings are ordered, are ordered lexicographically.

To solve this, you should add a new timestamp property to each of your objects as explained here. Assuming that those objects are within a node named dates, then just simply use a query like this:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef.child("dates").orderByChild("timestamp");
query.addListenerForSingleValueEvent(/* ... */);
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193