Search code examples
androidfirebasefirebase-realtime-databasefirebaseui

Get data from Firebase and make it a top 100 ranking activity


here my codes:

 private RecyclerView recycleRank;
private LinearLayoutManager layManRank;
private FirebaseRecyclerAdapter<Word, RankingActivity.RankingViewHolder> recAdapterRank;

...

DatabaseReference personsRef = FirebaseDatabase.getInstance().getReference("Word");
    Query personsQuery = personsRef.orderByChild("count");

    recycleRank.hasFixedSize();
    layManRank = new LinearLayoutManager(this);
    layManRank.setReverseLayout(true);
    layManRank.setStackFromEnd(true);
    recycleRank.setLayoutManager(layManRank);

So with .setReverseLayout the counts are displayed in descending order. Without that, it starts with the smallest number and goes down to the highest. But it should be the other way around.

recAdapterRank = new FirebaseRecyclerAdapter<Word, RankingViewHolder>(personsOptions) {
        @Override
        protected void onBindViewHolder(RankingActivity.RankingViewHolder holder, final int position, final Word model) {
            holder.setWord(model.getWord());
            long score = model.getCount();
            holder.setScore(String.valueOf(score));
            holder.setRank(String.valueOf(holder.getAdapterPosition()));
        }

holder.setRank(String.valueOf(holder.getAdapterPosition())); Here, the respective number is output in the ranking.

So the Activity should looks like this

# Word Score 1 Tree 12 2 Wood 9

But in my case, because the list is reversed, the numbering starts at the highest number and goes to 0.

How can I handle that? Do I need an Int Array or something like that? And how is it possible to make the ranking only 100 Words long? Currently there are all words from the database in the ranking.

Thank you guys


Solution

  • Ok so you can handle that.

    Query personsQuery = personsRef.orderByChild("count").limitToLast(100);
    

    Thanks to @Hocine B

    And than to reverse it you can use:

    recAdapterRank = new FirebaseRecyclerAdapter<Word, RankingViewHolder>(personsOptions) {
        @Override
        protected void onBindViewHolder(RankingActivity.RankingViewHolder holder, final int position, final Word model) {
            holder.setWord(model.getWord());
            long score = model.getCount();
            holder.setScore(String.valueOf(score));
    
            //Here is the Code
            int realRank = 100 - holder.getAdapterPosition();
    
            holder.setRank(String.valueOf(realRank));
        }